首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >java.lang.SecurityException:权限拒绝:在Android中阅读com.android.providers.media.MediaProvider,同时从图库拍照

java.lang.SecurityException:权限拒绝:在Android中阅读com.android.providers.media.MediaProvider,同时从图库拍照
EN

Stack Overflow用户
提问于 2016-06-07 14:49:08
回答 3查看 55.7K关注 0票数 21

我正在尝试从图库中选择图像,但我的应用程序收到异常消息“出了问题”。我以为我正确地设置了android的WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE权限,但是我一直收到错误,我应该怎么做才能让它正常工作?

这是我的Log cat错误

代码语言:javascript
复制
06-07 12:07:27.567    1692-1711/? E/DatabaseUtils﹕ Writing exception to parcel
    java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/359 from pid=2818, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
            at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
            at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
            at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
            at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
            at android.os.Binder.execTransact(Binder.java:453)

这是我的活动代码

代码语言:javascript
复制
public class MainActivity extends Activity {
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                ImageView imgView = (ImageView) findViewById(R.id.imgView);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(BitmapFactory
                        .decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

以下是我的Menifest.xml文件代码

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tazeen.image_fromgallery" >

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

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

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

</manifest>
EN

回答 3

Stack Overflow用户

发布于 2016-06-07 14:51:43

对于您的问题,您有两种解决方案。最快的方法是将targetApi降低到22 (build.gradle文件)。其次是使用新的runtimePermission模型:由于您的目标api是23,因此您还应该在运行时添加权限。

代码语言:javascript
复制
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.READ_EXTERNAL_STORAGE) {

    // Should we show an explanation?
    if (shouldShowRequestPermissionRationale(
            Manifest.permission.READ_EXTERNAL_STORAGE)) {
        // Explain to the user why we need to read the contacts
    }

    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

    // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
    // app-defined int constant

    return;
}

在此处找到Sniplet:https://developer.android.com/training/permissions/requesting.html

票数 7
EN

Stack Overflow用户

发布于 2017-09-26 09:40:08

您需要显式地将权限设置为与您的意图匹配的所有包。您可以使用此实用程序来执行此操作:

代码语言:javascript
复制
List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        getContext().grantUriPermission(packageName, imageFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
票数 2
EN

Stack Overflow用户

发布于 2018-05-12 22:04:07

有一个简单的方法,设置一个动作到你的"galleryIntent“,这样操作系统就不会死机了(我在Andorid Emulator API23上测试了一下,它工作了):

代码语言:javascript
复制
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

另外,也不要忘了设置一个类型:

代码语言:javascript
复制
galleryIntent.setType("image/*");
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37672338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档