Android Oreo: URI จาก ACTION_GET_CONTENT ไม่ได้รับ FLAG_GRANT_READ_URI_PERMISSION เมื่อห่อด้วย Intent.createChooser

ฉันได้รับข้อยกเว้นด้านล่างเมื่อใช้ Intent ACTION_GET_CONTENT ที่ห่อด้วย Intent.createChooser ฉันไม่ได้รับข้อยกเว้นนี้เมื่อฉันไม่รวม Intent ด้วย Intent.createChooser..

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>

วิธีแก้ไขปัญหานี้เมื่อใช้ตัวเลือก


person Thanigs    schedule 20.06.2019    source แหล่งที่มา
comment
ลองดูที่นี้ =› stackoverflow.com/questions/56554617/   -  person Chirag Savsani    schedule 20.06.2019
comment
ฉันไม่มีตัวจัดการไฟล์ URI ก่อนที่จะเปิดตัว Intent วัตถุประสงค์ในการเปิดตัว Intent คือให้ผู้ใช้เลือกไฟล์ ฉันได้รับผู้ให้บริการ uri ที่ onActivityResult เท่านั้น   -  person Thanigs    schedule 20.06.2019