2017-06-22 13 views
0

私はSDカードから曲にアクセスしようとする音楽プレーヤーを作成しています。しかし、それは許可に関連する何らかのエラーを示しています。許可が与えられていることを確認するにはどうすればいいですか?私はアンドロイドの初心者です。どんな助けもありがとう。外部のSDカードの許可を求める方法は?

これは私が曲にアクセスする方法です。

 public void getSongList(){ 

    ContentResolver musicResolver = getContentResolver(); 
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 

    if(musicCursor!=null && musicCursor.moveToFirst()){ 

     int titleColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.TITLE); 
     int idColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media._ID); 
     int artistColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.ARTIST); 

     do { 
      long thisId = musicCursor.getLong(idColumn); 
      String thisTitle = musicCursor.getString(titleColumn); 
      String thisArtist = musicCursor.getString(artistColumn); 
      songList.add(new Song(thisId, thisTitle, thisArtist)); 
     } 
     while (musicCursor.moveToNext()); 
    } 
} 

私はonCreateでリストをソートしています。

 protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //retrieve list view 
    songView = (ListView)findViewById(R.id.song_list); 
    //instantiate list 
    songList = new ArrayList<Song>(); 
    //get songs from device 
    getSongList(); 
    //sort alphabetically by title 
    Collections.sort(songList, new Comparator<Song>(){ 
     public int compare(Song a, Song b){ 
      return a.getTitle().compareTo(b.getTitle()); 
     } 
    }); 

@hasbiで提案されているように関数を追加しても、同じエラーが発生します。これは私のlogcatサマリーです。

FATAL EXCEPTION: main 
                     Process: com.example.john.musicplayer, PID: 2716 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.john.musicplayer/com.example.john.musicplayer.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2716, uid=10083 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                     Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2716, uid=10083 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() 
                      at android.os.Parcel.readException(Parcel.java:1683) 
                      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:530) 
                      at android.content.ContentResolver.query(ContentResolver.java:472) 
                      at com.example.john.musicplayer.MainActivity.getSongList(MainActivity.java:147) 
                      at com.example.john.musicplayer.MainActivity.onCreate(MainActivity.java:63) 
                      at android.app.Activity.performCreate(Activity.java:6662) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:154)  
                      at android.app.ActivityThread.main(ActivityThread.java:6077)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  
+1

から言及されているコードをチェックすることは無意味な問題の説明です。あなたは**特定**である必要があります。 *エラーが表示されている場合、エラーメッセージが表示されます。それはあなたの質問に属しているので、我々はその情報も持っています。 See [ask]。 –

+0

@KenWhite特定できないことをお詫び申し上げます。私はlogcatの概要を追加しました – Apurva

答えて

0

mainactivity

ActivityCompat.requestPermissions(MainActivity.this, 
       new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
       1); 

のあなたのOnCreate関数にこのコードを追加するには、マニフェストファイルに

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

を追加するには、mainactivityファイルのグローバル領域にこのコードを追加

@Override 
public void onRequestPermissionsResult(int requestCode, 
            String permissions[], int[] grantResults) 
{ 
    switch (requestCode) { 
    case 1: { 

     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      // permission was granted, yay! Do the 
      // contacts-related task you need to do.   
     } else { 

      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
      Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); 
     } 
     return; 
    } 

    // other 'case' lines to check for other 
    // permissions this app might request 
} 
}` 
+0

ありがとう@hasbi。私はこれで新しいです。あなたは、マニフェストでそれを言及した後でさえも、メインファイルで許可許可を再度チェックする必要がある理由について説明できますか?なぜ許可が与えられないのでしょうか? – Apurva

+0

Android Mの後、アプリはアプリの実行中に許可を得る必要があります。したがって、マニフェストとアクティビティの両方のアクセス許可を追加する必要があります。 – hasbi

+0

さらに、関数を追加した後も、私はまだ同じエラーが発生しています。私はlogcatの要約を投稿しました。実行時のアクセス許可を追加した後で – Apurva

0

logcatでは、それは、親切に明確なアプリのデータを許可拒否を示すとrecheck.Alsoだが、再び*いくつかのエラーを示す* @hasbi

関連する問題