Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get read external storage permissions?

I'm developing an mp3 player application for android and I'm getting an error related to user permissions(READ_EXTERNAL_STORAGE)

This is my code where I ask for permissions:

int permissionCheck = ContextCompat.checkSelfPermission(PlayListActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);

        if (ContextCompat.checkSelfPermission(PlayListActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {


            // OPCIONAL(explicaciones de poque pedimos los permisos)
            if (ActivityCompat.shouldShowRequestPermissionRationale(PlayListActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {

            } else {
                //pedir permisos
                ActivityCompat.requestPermissions(PlayListActivity.this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        permissionCheck);

and this is the rest of the code:

ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

    SongsManager plm = new SongsManager(PlayListActivity.this);
    // get all songs from sdcard
    this.songsList = plm.getPlayList();

    // looping through playlist
    for (int i = 0; i < songsList.size(); i++) {
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);

        // adding HashList to ArrayList
        songsListData.add(song);
    }

    // se añaden las canciones a la ListView mediante un adapter utilizando el layout playlist_item
    ListAdapter adapter = new SimpleAdapter(this, songsListData,
            R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
            R.id.songTitle });

    setListAdapter(adapter);

this is the error:

03-31 22:09:16.159 2258-2258/com.androidchatapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.androidchatapp, PID: 2258
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidchatapp/com.androidchatapp.PlayListActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2258, uid=10184 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
    at android.app.ActivityThread.access$900(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5451)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2258, uid=10184 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1620)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
    at android.content.ContentResolver.query(ContentResolver.java:493)
    at android.content.ContentResolver.query(ContentResolver.java:435)
    at com.androidchatapp.SongsManager.getPlayList(SongsManager.java:48)
    at com.androidchatapp.PlayListActivity.onCreate(PlayListActivity.java:65)
    at android.app.Activity.performCreate(Activity.java:6323)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
    at android.app.ActivityThread.access$900(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5451)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
like image 494
Muse App Avatar asked Mar 31 '17 20:03

Muse App


People also ask

How do I get read external permissions on Android?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

How do I manage external storage permissions in Android?

On the Settings > Privacy > Permission manager > Files and media page, each app that has the permission is listed under Allowed for all files. If your app targets Android 11, keep in mind that this access to "all files" is read-only.


2 Answers

Android introduce runtime permission from Marshmallow OS, so we need to implement runtime permission with Manifest permission. if CompileSdkVersion = 23

You can ask permission when your screen open (in onCreate()) or click event of your button (Like -play Music )

use this method for check runtime Permission is enable or not

 private static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 41;
 public boolean checkPermissionForReadExtertalStorage() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int result = context.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }
    return false;
 }

if it return false then call below method, it will show a dialog for permission

 public void requestPermissionForReadExtertalStorage() throws Exception {
        try {
            ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                    READ_STORAGE_PERMISSION_REQUEST_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
like image 175
Nishchal Sharma Avatar answered Sep 17 '22 23:09

Nishchal Sharma


You have asked the permission from the code , but you are probably forgetting to add this permission tag in the manifest file

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
like image 33
Faisal Avatar answered Sep 16 '22 23:09

Faisal