Android无权阅读内容:// media / external / audio / media

时间:2015-10-27 12:05:40

标签: xml audio permissions android-6.0-marshmallow

在我的app api级别targetSdkVersion 23.我添加了以下权限:

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

            <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:supportsRtl="true"
                android:theme="@style/AppTheme" >
                <activity android:name=".MetaExtractActivity" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

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

        </manifest>

当我尝试执行以下内容时:

    String[] STAR = {"*"};
    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    //  Uri allsongsuri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    Cursor cursor = null;


    try {

      this.grantUriPermission("my.mymetaextractor",        
      allsongsuri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION |     
     Intent.FLAG_GRANT_READ_URI_PERMISSION);
        cursor = managedQuery(allsongsuri, STAR, selection, null,  
    null);
    } catch (Exception e) {
        String msg = e.getMessage();
        Log.i("tag", "wait");
    }

我得到例外

  

java.lang.SecurityException:Uid 10209没有uri的权限   0 @ content:// media / external / audio / media

但是Uri allsongsuri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;有效。 Uri allsongsuri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

的例外情况

请注意,相同的代码在较旧的设备android 4中运行良好。但它发生在Android 6.我假设一些新的android 6添加权限。请问如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您需要在运行时不仅在清单permissions

中询问此权限

if(ContextCompat.checkSelfPermission(this,&#34; android.permission.WRITE_EXTERNAL_STORAGE&#34;)!= PackageManager.PERMISSION_GRANTED) requestPermissions(new String [] {&#34; android.permission.READ_PHONE_STATE&#34;},0);