尽管添加了权限,MediaStore的SecurityException

时间:2015-10-07 02:26:36

标签: android permissions mediastore

我正在尝试使用Android的MediaStore,但即使在添加(我认为是)正确的权限之后,我仍然会遇到权限问题。

这是最新的SDK,6.0(API 23)和运行6.0的设备。

这是我最小的活动:

package com.example.myapplication;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.webkit.MimeTypeMap;

public class MainActivity extends AppCompatActivity {

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

        ContentResolver cr = getContentResolver();

        Uri uri = MediaStore.Files.getContentUri("external");
        String[] projection = null;
        String sortOrder = null;
        String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp3");
        String[] selectionArgsMp3 = new String[]{ mimeType };
        Cursor allmp3Files = cr.query(uri, projection, selectionMimeType, selectionArgsMp3, sortOrder);
    }
}

和清单:

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

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

    <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>
    </application>
</manifest>

这是我得到的例外:

10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime: Process: com.example.myapplication, PID: 579
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/file from pid=579, uid=10119 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:      Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/file from pid=579, uid=10119 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:491)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:434)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at com.example.myapplication.MainActivity.onCreate(MainActivity.java:33)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-06 19:16:50.662 579-579/com.example.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

有什么想法吗?非常感谢!

0 个答案:

没有答案