对intent-filter的读取权限

时间:2015-10-28 18:53:41

标签: android android-permissions

我有一个使用getExternalFilesDir()目录的应用,所以我在清单中指定了正确的权限:

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

我指定了maxSdkVersion,因为自API 19起,getExternalFilesDir()不再需要这些权限。

我还定义了一个intent-filter,允许用户使用我的应用程序打开PDF文件,然后导入它们(反过来,它会复制它并将其放入我的应用程序特定目录中)。 / p>

final Uri pdfUri = intent.getData();  // intent is from getIntent()
InputStream is = getContentResolver().openInputStream(pdfUri); // Error is here

现在,由于我将maxSdkVersion值添加到我的权限中,每当我尝试在API 19+上导入PDF(从我的下载文件夹中)时,我都会得到一个整洁的Permission Denied错误。

W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Download/field test v13.pdf: open failed: EACCES (Permission denied)
W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:496)
W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:103)
W/System.err﹕ at android.content.ContentResolver.openInputStream(ContentResolver.java:660)
W/System.err﹕ at com.myapp.ImportPDF$1.onClick(ImportPDF.java:96)

如何获得读取用户明确要求我的应用处理的文件的权限?当然有处理这类案件的事情,但我很难在其上找到任何东西。

1 个答案:

答案 0 :(得分:0)

For reading from user's device you need the READ_EXTERNAL_STORAGE permission on all the api levels. The only case that you wouldn't need it is if the app you launch to give you the reference, gives it back as a content URI and not file URI. Also if you want to write your files on anywhere other than your application folder, you need WRITE_EXTERNAL_STORAGE and if you need to ask for WRITE_EXTERNAL_STORAGE then the you also get READ permission automatically.

From 23 (6.0) and above, you are required to ask these permissions in runtime and for doing so you can use this library to help you with it.