1

I am creating the media viewer app for android.When my app is running on mobile is works great, but when I run my app on tablet it crashes.I always get such exception:

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=26136, uid=10096 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

here is my code:

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;
}
}

manifest code:

 <?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>
nullbyte
  • 1,178
  • 8
  • 16

2 Answers2

0

set permission on 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" />

Vishnu
  • 56
  • 1
  • 12
0

By the looks of it you have set up the permissions correctly in your manifest. This crash might only happen on Android-M API23. The reason for that is READ_EXTERNAL_STORAGE is considered a Dangerous permission. So even though you have declared it in the Manifest, you need make sure to get the user to accept this permission at runtime. How to do this? Read here: http://developer.android.com/training/permissions/requesting.html

This is the right way to do it, but if you want to take the short way around, you could set the targetSdkVersion to 22 or lower. This will ensure that even when running on the M version, all permissions are granted. But still the user can manually turn off these permissions. So that's the downfall.

Note: This lesson describes how you implement permissions requests on apps that target API level 23 or higher, and are running on a device that's running Android 6.0 (API level 23) or higher. If the device or the app's targetSdkVersion is 22 or lower, the system prompts the user to grant all dangerous permissions when they install or update the app.

Henry
  • 17,490
  • 7
  • 63
  • 98