本文介绍了异常java.lang.SecurityException:读取..MediaDocumentsProvider ...需要android.permission.MANAGE_DOCUMENTS或grantUriPermission()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在尝试为个人资料图片选择图片时,我才在某些设备中发现此问题.在模拟器中签入时,这些问题没有出现,但在应用程序的实时版本中,此用户面临此问题. 从firebase崩溃报告中捕获的屏幕截图.请帮助我找出问题所在.

I found this issue only in some devices when trying to pick the picture for the profile image. While checking in the emulator these issue are not seen but on live version of the app this issues are facing by this users. Screenshot captured from the firebase crash report. Please help me to figure out the issue.

AndroidMenifest.xml

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

SomeActivity.java

private void pickProfilePictureTask() {

    Intent intent = null;
    if (Build.VERSION.SDK_INT > 19) {

        intent = new Intent(Intent.ACTION_OPEN_DOCUMENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.addCategory(Intent.CATEGORY_OPENABLE);

    } else {

        intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }

    intent.setType("image/*");
    startActivityForResult(intent, IMAGE_PICKER_REQUEST);


}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_PICKER_REQUEST && resultCode == Activity.RESULT_OK) {


        if (data != null) {
            /**Getting bitmap
             *
             * InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
             * Bitmap userPicBitmap = BitmapFactory.decodeStream(inputStream);
             */
            File file = new File(getFileNameFromURI(data.getData()));
            String imageName = file.getPath();
            uploadProfilePicture.setText(imageName);
            userProfilePicUri = data.getData() + "";

        }


    }
}

private String getFileNameFromURI(Uri contentURI) {
    String result;
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        result = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

Android 5.1.1中的错误 异常java.lang.SecurityException:权限被拒绝:读取com.android.providers.media.MediaDocumentsProvider uri content://com.来自pid = 19207,uid = 10260的android.providers.media.documents/document/image%3A6617 需要android.permission.MANAGE_DOCUMENTS或grantUriPermission()

ERROR in Android 5.1.1 Exception java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image%3A6617 from pid=19207, uid=10260 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

Android 8.0.0中的错误异常java.lang.SecurityException:权限拒绝:从ProcessRecord {5c1039e 5061:com.mobotechnology.cvmaker/u0a219打开提供程序com.android.providers.media.MediaDocumentsProvider }(pid = 5061,uid = 10219)要求您使用ACTION_OPEN_DOCUMENT或相关API获得访问权限

ERROR in Android 8.0.0 Exception java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{5c1039e 5061:com.mobotechnology.cvmaker/u0a219} (pid=5061, uid=10219) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

推荐答案

这对于具有file方案的Uri将起作用.对于使用content方案的大多数Uri值,它将不起作用.这些更类似于经过身份验证的网站的HTTP URL—.在您的会话处于活动状态时,URL很好,但是之后就没用了.

That will work for a Uri with a file scheme. It will not work for most Uri values with a content scheme. Those are more akin to an HTTP URL to an authenticated Web site — the URL is good while your session is alive but is useless afterwards.

如果是通过ACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENTACTION_OPEN_DOCUMENT_TREE 获得的Uri,则在ContentResolver上调用takePersistableUriPermission()来提供Uri然后,您可以放心地保存该Uri.您将无限期地访问内容,直到用户撤消访问权限或内容发生显着变化(例如,被删除),以致其Uri不再有效.

If you obtain the Uri via ACTION_OPEN_DOCUMENT, ACTION_CREATE_DOCUMENT, or ACTION_OPEN_DOCUMENT_TREE, and you call takePersistableUriPermission() on ContentResolver supplying the Uri, then you can safely persist that Uri. You will have access to the content indefinitely, until the user revokes access or the content significantly changes (e.g., is deleted) such that its Uri is no longer valid.

任何其他content Uri(例如ACTION_GET_CONTENT)都很好..在这里,您唯一的选择是将内容复制到您控制的某个文件中(例如,在getCacheDir()中),然后保存该文件的路径.

Any other content Uri (e.g., ACTION_GET_CONTENT) is only good for a very short time. Your only option here is to copy the content to some file that you control (e.g., in getCacheDir()) and save the path to that file.

这篇关于异常java.lang.SecurityException:读取..MediaDocumentsProvider ...需要android.permission.MANAGE_DOCUMENTS或grantUriPermission()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:17
查看更多