java.lang.SecurityException: Permission Denial: reading...requires android.permi

I have been busy with Gallery, and recently encountered such a bug, in Setting-->storage&USB->internal storage->images->select a-picture>home key exit, then delete the selected photo in Gallery, and finally in the recent Select Setting in the list, the picture still remains in that picture, click to print men, it will report Gallery Force Close. Through the log, you can see the following error:
04-15 11:00:33.959  8427  8427 D AndroidRuntime: Shutting down VM

04-15 11:00:33.965  8427  8427 E AndroidRuntime: FATAL EXCEPTION: main

04-15 11:00:33.965  8427  8427 E AndroidRuntime: Process: com.android.gallery3d, PID: 8427

04-15 11:00:33.965  8427  8427 E AndroidRuntime: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image%3A593 from pid=8427, uid=10065 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:1620)

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at android.content.ContentResolver.query(ContentResolver.java:493)

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at android.content.ContentResolver.query(ContentResolver.java:435)

04-15 11:00:33.965  8427  8427 E AndroidRuntime: 	at com.android.gallery3d.filtershow.cache.ImageLoader.getLocalPathFromUri(ImageLoader.java:85)



Before deleting this photo, it is normal to choose to print. Why do I report a permission problem after deleting the photo? After code analysis, Gallery does not have permission to access MediaDocumentsProvider, MediaDocumentsProvider can accept android:grantUriPermissions="true" transfer permissions, and the file manager documentsui has applied for <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" / >, and the file manager will open the image and pass the read permission to the Gallery through intent: Intent.FLAG_GRANT_READ_URI_PERMISSION

final Intent view = new Intent(Intent.ACTION_VIEW);
            view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            view.setData(doc.derivedUri);

            try {
                startActivity(view);
            } catch (ActivityNotFoundException ex) {
                Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
            }

http://androidxref.com/6.0.0_r1/xref/frameworks/base/packages/DocumentsUI/src/com/android/documentsui/DocumentsActivity.java
so that Gallery can access MediaDocumentsProvider, and when a photo is deleted, this The permission deletes MediaDocumentsProvider.onMediaStoreDelete(getContext(),
4069 volumeName, FileColumns.MEDIA_TYPE_IMAGE, id);, so the permission problem is reported when accessing again
if (mediaType == FileColumns.MEDIA_TYPE_IMAGE) {
                                    deleteIfAllowed(uri, data);
                                    MediaDocumentsProvider.onMediaStoreDelete(getContext(),
                                            volumeName, FileColumns.MEDIA_TYPE_IMAGE, id);

                                    idvalue[0] = String.valueOf(id);
                                    database.mNumQueries++;
                                    Cursor cc = db.query("thumbnails", sDataOnlyColumn,
                                                "image_id=?", idvalue, null, null, null);
                                    try {
                                        while (cc.moveToNext()) {
                                            deleteIfAllowed(uri, cc.getString(0));
                                        }
                                        database.mNumDeletes++;
                                        db.delete("thumbnails", "image_id=?", idvalue);
                                    } finally {
                                        IoUtils.closeQuietly(cc);
                                    }
                                }

http://androidxref.com/6.0.0_r1/xref/packages/providers/MediaProvider/src/com/android/providers/media/MediaProvider.java


revokeUriPermission remove permission
/**
     * When deleting an item, we need to revoke any outstanding Uri grants.
     */
    static void onMediaStoreDelete(Context context, String volumeName, int type, long id) {
        if (!"external".equals(volumeName)) return;

        if (type == FileColumns.MEDIA_TYPE_IMAGE) {
            final Uri uri = DocumentsContract.buildDocumentUri (
                    AUTHORITY, getDocIdForIdent(TYPE_IMAGE, id));
            context.revokeUriPermission(uri, ~0);
        } else if (type == FileColumns.MEDIA_TYPE_VIDEO) {
            final Uri uri = DocumentsContract.buildDocumentUri (
                    AUTHORITY, getDocIdForIdent(TYPE_VIDEO, id));
            context.revokeUriPermission(uri, ~0);
        } else if (type == FileColumns.MEDIA_TYPE_AUDIO) {
            final Uri uri = DocumentsContract.buildDocumentUri (
                    AUTHORITY, getDocIdForIdent(TYPE_AUDIO, id));
            context.revokeUriPermission(uri, ~0);
        }
    }


http://androidxref.com/6.0.0_r1/xref/packages/providers/MediaProvider/src/com/android/providers/media/MediaDocumentsProvider.java

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326769162&siteId=291194637