我正在處理音樂播放器應用程序所以我必須從存儲卡中獲取所有歌曲,並且我成功地從存儲卡應用程序獲取歌曲並在棒棒糖上工作。棉花糖權限錯誤
問題 棉花糖運行時間允許,我還添加運行時間許可,棉花糖,但是應用程序仍然會崩潰..
這是我的全部代碼,請告訴我,我必須寫運行時間許可棉花糖。
public class MainActivity extends AppCompatActivity {
private int STORAGE_PERMISSION_CODE = 23;
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_phone_music_grid();
if(isReadStorageAllowed()){
//If permission is already having then showing the toast
Toast.makeText(MainActivity.this,"You already have the permission",Toast.LENGTH_LONG).show();
//Existing the method with return
return;
}
//If the app has not the permission then asking for the permission
requestStoragePermission();
}
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
}
//And finally ask for the permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE);
}
//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if(requestCode == STORAGE_PERMISSION_CODE){
//If permission is granted
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Displaying a toast
Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
}else{
//Displaying another toast if permission is not granted
Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
}
}
}
private boolean isReadStorageAllowed() {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED)
return true;
return false;
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private AdapterView.OnItemClickListener musicgridlistener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv.setTextColor(Integer.parseInt(String.valueOf(R.color.colorAccent)));
tv = (TextView) convertView;
return tv;
}
}}
請不要馬克複製只需要修正我的權限 感謝
崩潰報告: E/AndroidRuntime:致命異常:主要 過程:com.example.j.musicapp,PID: 28606 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.j.musicapp/com.example.j.musicapp.MainActivity}:java.lang.SecurityException:權限拒絕:閱讀com.android.providers.media .MediaProvider uri content:// media/external/audio/media from pid = 2 8606,uid = 10161需要android.permission.READ_EXTERNAL_STORAGE或者grantUriPermission() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.access $ 1100(ActivityThread.java:229) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7331) at java.lang.reflect.Method.invoke(Native Metho d) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java。 lang.SecurityException:權限拒絕:從pid = 28606,uid = 10161需要android.permission.READ_EXTERNAL_STORAGE或grantUriPermission() android.os.Parcel.readException(Parcel.java:1621) 在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) 在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 在機器人。 content.Conte ntProviderProxy.query(ContentProviderNative.java:421) at android.content.ContentResolver.query(ContentResolver.java:502) at android.content.ContentResolver.query(ContentResolver.java:445) at android.app.Activity。 managedQuery(Activity.java:2227) at com.example.j.musicapp.MainActivity.init_phone_music_grid(MainActivity.java:100) at com.example.j.musicapp.MainActivity.onCreate(MainActivity.java:35) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(Acti vityThread.java:3266) at android.app.ActivityThread.handleLaunchActivity(ActivityThread。java:3415) at android.app.ActivityThread.access $ 1100(ActivityThread.java:229) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler .java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7331) at java.lang.reflect.Method.invoke(Native法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
發佈logcat的細節 –
http://stackoverflow.com/a/34959331/4919210檢查這個答案! –
好吧,先生等待............ –