Android Oreo:使用Intent.createChooser包装时,未使用FLAG_GRANT_READ_URI_PERMISSION授予ACTION_GET_CONTENT中的URI

时间:2019-06-20 05:25:04

标签: android android-8.0-oreo

使用Intent.createChooser包装的Intent ACTION_GET_CONTENT时,出现以下异常。当我不使用Intent.createChooser包装Intent时,我不会收到此异常。

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/523 flg=0x1 (has extras) }} to activity {com.mycompany.app/com.mycompany.app.activity.MessageCompose}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.downloads.DownloadStorageProvider from ProcessRecord{..} (pid=20538, uid=10127) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

有问题的代码:-

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(type);
i = Intent.createChooser(i, "Select a file");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(i, ACTIVITY_REQUEST_PICK_ATTACHMENT);

删除行后代码有效

i = Intent.createChooser(i, "Select a file"); 

当我检查onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
uri = data.getData();

int permissionGrant = getApplicationContext().checkCallingOrSelfUriPermission(uri,  Intent.FLAG_GRANT_READ_URI_PERMISSION); 
// Returns PERMISSION_GRANTED without createChooser 
// Returns PERMISSION_DENIED with createChooser

清单文件:-

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mycompany.app.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/providers_paths"/>
        </provider>

使用选择器时如何解决此问题。

0 个答案:

没有答案