总是得到java.lang.SecurityException:Permission Denial试图从外部存储中读取数据

时间:2015-11-11 09:16:17

标签: java android exception media

我正在为Android创建媒体查看器应用程序。当我的应用程序在移动设备上运行时效果很好,但是当我在平板电脑上运行我的应用程序时它会崩溃。我总是遇到这样的异常:

  

java.lang.SecurityException:Permission Denial:读取com.android.providers.media.MediaProvider uri content:// media / external / audio / media from pid = 26136,uid = 10096需要android.permission.READ_EXTERNAL_STORAGE,或者grantUriPermission()

这是我的代码:

SortedSet<String> dirList=new TreeSet<String>();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.DATA};
Cursor cursor = null;
if (uri != null)
    cursor= context.getContentResolver().query(uri,projection,null,null,null);
if (cursor != null && cursor.moveToFirst()) {
     do {
        String tempDir = cursor.getString(0);
        tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
        try {
            dirList.add(tempDir);
        } catch(Exception e) {
            e.printStackTrace();
        }
    } while (cursor.moveToNext());
    directories = new String[dirList.size()];
    dirList.toArray(directories);
    directories=mediaFilter.filterMediaFiles(directories);
    if(!isInstalled){
        isInstalled=true;
        installAudioDetails();
    }
    return true;
}
}

清单代码:

 <?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vasylpaliy.mediaview">
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <application
        android:allowBackup="true"
        android:largeHeap="true"
        android:icon="@drawable/gallery_icon"
        android:label="@string/app_name"
        android:theme="@style/SelectTheme">
        <activity
            android:name=".MainActivity"
            android:theme="@style/ImageTheme"
            android:label="@string/photos">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ImageSelecter"
            android:parentActivityName=".MainActivity"
            android:label="@string/photos"
            android:theme="@style/SelectTheme">
            <intent-filter>
                <action android:name="android.intent.action.ImageSelecter" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Settings"
            android:label="Setting"
            android:parentActivityName="com.example.vasylpaliy.mediaview.ImageSelecter">
            <intent-filter>
                <action android:name="android.intent.action.FullScreenImage" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AudioList"
            android:theme="@style/AudioListTheme"
            android:label="@string/audio_list">
        </activity>
        <activity
            android:name=".AudioAction"
            android:icon="@drawable/red_music"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@style/AudioPlayerTheme"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:parentActivityName="com.example.vasylpaliy.mediaview.AudioList"
            android:label="@string/audio_view">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <data android:mimeType="audio/*"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".ImageSlider"
            android:theme="@style/ImageSlider"
            android:parentActivityName=".ImageSelecter"
            android:label="Image">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.audioplayer.AudioPlayer"
            android:theme="@style/AudioPlayerTheme">
        </activity>
        <service
            android:name="com.audioplayer.AudioService">
        </service>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

设置 AndroidManifest.xml

的权限
<?xml version="1.0" encoding="utf-8"?>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".cst_cell_album_list"
        android:label="@string/title_activity_cst_cell_album_list" >
    </activity>
    <service android:enabled="true" android:name="com.vvs.root.memusic.PlayerService" />
    <receiver android:name="com.vvs.root.memusic.PlayerService$MyReceiver" >

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

答案 1 :(得分:0)

通过它的外观,您可以在清单中正确设置权限。此崩溃可能只发生在Android-M API23上。原因是READ_EXTERNAL_STORAGE被视为Dangerous permission。因此,即使您已在Manifest中声明它,您也需要确保让用户在运行时接受此权限。这该怎么做?请在此处阅读:http://developer.android.com/training/permissions/requesting.html

这是正确的方法,但是如果您想要缩短范围,可以将targetSdkVersion设置为22或更低。这将确保即使在M版本上运行,也会授予所有权限。但是用户仍然可以手动关闭这些权限。这就是垮台。

  

注意:本课程介绍如何实施权限请求   针对API级别23或更高级别并且正在设备上运行的应用程序   运行Android 6.0(API级别23)或更高版本。如果设备或   应用程序的targetSdkVersion为22或更低,系统会提示用户   在安装或更新时授予所有危险权限   应用