web-dev-qa-db-ja.com

外部ストレージの読み取り許可を取得する方法は?

Android=のmp3プレーヤーアプリケーションを開発しています。ユーザー権限に関連するエラーが発生しています(READ_EXTERNAL_STORAGE)

これは私が許可を求めるコードです:

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

そして、これは残りのコードです:

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

これはエラーです:

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)
5
Muse App

あなたはコードから許可を求めましたが、おそらくこの許可タグをマニフェストファイルに追加するのを忘れているでしょう

<uses-permission Android:name="Android.permission.READ_EXTERNAL_STORAGE"/>
11
Faisal