简体   繁体   English

读取内部下载文件夹中的文件 Android R

[英]Read file in internal Download folder Android R

on Android R i'm fine to write file in Download folder.在 Android R 上,我可以在下载文件夹中写入文件。 If i try to read file i get IOException access denied.如果我尝试读取文件,我会拒绝 IOException 访问。

zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/Download/saves.zip")));

What is the right way to read file on Android R or from what folder can i read without problem?在 Android R 或从哪个文件夹读取文件的正确方法是什么?

I'm using我在用着

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

android:requestLegacyExternalStorage=”true”

EDIT: New Code编辑:新代码

ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, "saves.zip");
contentValues.put(MediaStore.Downloads.MIME_TYPE, "application/zip");
Uri uri = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            uri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL);
        }
        Uri itemUri = getContentResolver().insert(uri, contentValues);

zis = new ZipInputStream(getContentResolver().openInputStream(itemUri));

Starting in Android 11, apps that use the scoped storage model can access only their own app-specific cache files.从 Android 11 开始,使用范围存储 model 的应用程序只能访问自己的应用程序特定缓存文件。

So better use app's shared data folder to write/read your files.所以最好使用应用程序的共享数据文件夹来写入/读取您的文件。

here is the full description.这是完整的描述。

https://developer.android.com/about/versions/11/privacy/storage#permissions-target-11 https://developer.android.com/about/versions/11/privacy/storage#permissions-target-11

on Android R i'm fine to write file in Download folder.在 Android R 上,我可以在下载文件夹中写入文件。

If your app can write a file somewhere than it can certainly read that file.如果您的应用程序可以在某处写入文件,那么它肯定可以读取该文件。

Your problem does not make sense.你的问题没有意义。

Is there anything you did not tell?有什么你没有告诉的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM