微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

权限拒绝:打开提供程序com.android.providers.media.MediaDocumentsProvider

我试图允许用户访问他的照片库以获取个人资料图片,并将该个人资料图片保存到SharedPreferences.我还有一个导航栏,可从SharedPreferences获取图片

我收到以下错误

java.lang.SecurityException: 
Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord
{a601c1c 3379:com.example.anishdalal.finalapp/u0a60} (pid=3379, uid=10060) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

这是获取图片代码

主要活动

       ImageView prof_pic = (ImageView) header.findViewById(R.id.profPic);
        pref = getSharedPreferences(Profile.pref_filename, 0);
        String uri = pref.getString("target_uri", "");
        TextView tv_name = (TextView) header.findViewById(R.id.tv_name);
        String name = pref.getString("name", "");
        if(!uri.equals("")) {
            Uri urii = Uri.parse(uri);
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(urii));
                tv_name.setText(name);
                prof_pic.setimageBitmap(bitmap);
            } catch (FileNotFoundException e) {

            }
        }
        else {
            prof_pic.setimageResource(R.drawable.ic_android_black_24dp);
        }
    }

profile_pic.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
                */
                Intent intent;

                if (Build.VERSION.SDK_INT < 19) {
                    intent = new Intent();
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.setType("*/*");
                    startActivityForResult(intent, KITKAT_VALUE);
                } else {
                    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("*/*");
                    startActivityForResult(intent, KITKAT_VALUE);
                }
            }
        });

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == KITKAT_VALUE) {
            if (resultCode == RESULT_OK) {
                targetUri = data.getData();
                //Bitmap bitmap;
                try {
                    bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri));
                /*
                SharedPreferences.Editor edit = pref.edit();
                edit.putString("target_uri", targetUri.toString());
                edit.apply();
                */
                    sTargetUri = targetUri.toString();
                    profile_pic.setimageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    // Todo Auto-generated catch block
                    e.printstacktrace();
                }
            }
        }
    }

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.finalapp">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".JobViewActivity"></activity>
    </application>

</manifest>

解决方法:

public static final int galLERY_INTENT_CALLED = 1;
    public static final int galLERY_KITKAT_INTENT_CALLED = 2;

if (Build.VERSION.SDK_INT <19){
                    Intent intent = new Intent();
                    intent.setType("*/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent, galLERY_INTENT_CALLED);
                } else {
                    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("*/*");
                    startActivityForResult(intent, galLERY_KITKAT_INTENT_CALLED);
                }



public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                Uri originalUri = null;
                if (Build.VERSION.SDK_INT < 19) {
                    originalUri = data.getData();
                } else {
                    originalUri = data.getData();
                    final int takeFlags = data.getFlags()
                            & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                    try {
                        getActivity().getContentResolver().takePersistableuriPermission(originalUri, takeFlags);
                    }
                    catch (SecurityException e){
                        e.printstacktrace();
                    }
                }
                try {
                    bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(originalUri));
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐