权限拒绝:android.permission.READ_EXTERNAL_STORAGE

时间:2016-05-11 12:07:41

标签: android permissions android-6.0-marshmallow

我在我的应用程序中使用MediaStore类并面临以下错误(logcat),

E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=5769, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:539)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:452)
at android.content.ContentProvider$Transport.query(ContentProvider.java:205)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:446)
05-11 17:27:35.982 5769-5769/com.media.wanware.fragmentdemo D/AndroidRuntime: Shutting down VM
05-11 17:27:35.984 5769-5769/com.media.wanware.fragmentdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.media.wanware.fragmentdemo, PID: 5769
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.media.wanware.fragmentdemo/com.media.wanware.fragmentdemo.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=5769, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
   at android.app.ActivityThread.access$800(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5254)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=5769, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
   at android.os.Parcel.readException(Parcel.java:1546)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
   at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
   at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
   at android.content.ContentResolver.query(ContentResolver.java:478)
   at android.content.ContentResolver.query(ContentResolver.java:422)
   at com.media.wanware.fragmentdemo.MainActivity.getAllSongsFromDevice(MainActivity.java:45)
   at com.media.wanware.fragmentdemo.MainActivity.onCreate(MainActivity.java:29)
   at android.app.Activity.performCreate(Activity.java:5990)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
   at android.app.ActivityThread.access$800(ActivityThread.java:151) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:135) 
   at android.app.ActivityThread.main(ActivityThread.java:5254) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我读了这个问题,

Android reading external storage gives securityException

但我已将权限正确地放在我的清单中,

的Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />

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

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

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

在进一步阅读后,我也经历了以下问题,

Android permission doesn't work even if I have declared it

  

现在没有得到你的许可的重要原因是因为   您的项目的targetSdkVersion为23或更高

     

对于这些权限,不仅是您的targetSdkVersion 23+应用   需要有元素,但你也必须   从Android 6.0+上的用户那里请求运行时的那些权限   设备,使用checkSelfPermission()和   requestPermissions()。

     

作为临时解决方法,请将targetSdkVersion降至23以下。

但我的目标SDK仅低于23。 为什么我收到同样的错误?

2 个答案:

答案 0 :(得分:2)

SDK 23具有处理Android权限的所有新方法和功能,以尝试使用较低的SDK版本编译代码。这可以解决您的问题。

并从您的权限中删除maxSdkVersion

快乐编码:)

答案 1 :(得分:0)

比使用较低版本进行编译更好,你必须为所有版本制作适当的代码,否则你最终将会落后于当前的编程方式,尤其是在android中。

查看此文章,了解如何在运行时here请求权限。

以下是我如何正确请求这些权限的示例:

if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?

            if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                    android.Manifest.permission.READ_EXTERNAL_STORAGE)) {

                Snackbar.make(getWindow().getDecorView().getRootView(), "We need permissions to do stuff.\n Please allow.", Snackbar.LENGTH_LONG)
                        .setAction("Settings", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                final Intent i = new Intent();
                                i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                i.addCategory(Intent.CATEGORY_DEFAULT);
                                i.setData(Uri.parse("package:" + context.getPackageName()));
                                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                                context.startActivity(i);
                            }
                        }).show();

                ActivityCompat.requestPermissions((Activity) context,
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION},
                        MY_PERMISSIONS_REQUEST_READ);

            } else {

                ActivityCompat.requestPermissions((Activity) context,
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION},
                        MY_PERMISSIONS_REQUEST_READ);

            }

        }else {
            //Do your stuff here 
        }