I have the following methods:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_songs);
ListView songsListView = findViewById(R.id.songList);
songsList = new ArrayList<Song>();
if (isStoragePermissionGranted()) {
getSongsList();
}
Collections.sort(songsList, new Comparator<Song>() {
@Override
public int compare(Song o1, Song o2) {
return o1.getTitle().compareTo(o2.getTitle());
}
});
SongAdapter adapter = new SongAdapter(this, songsList);
songsListView.setAdapter(adapter);
}
public void getSongsList(){
ContentResolver musicResolver = getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
@SuppressLint("Recycle")
Cursor musicCursor = musicResolver.query(musicUri,null,null,null,null);
if (musicCursor!=null && musicCursor.moveToFirst()){
int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do{
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songsList.add(new Song(thisId, thisTitle, thisArtist));
}while (musicCursor.moveToNext());
}
}
public boolean isStoragePermissionGranted() {
String TAG = this.getClass().getSimpleName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
Activity, which has this methods, should show a list of songs in internal storage in alphabet order. But when I run the program, I don't see anything in this activity. When I watched in logs, I didn't see anything with the tag of this activity, but when I watched in debugger, I saw, that method boolean isStoragePermissionGranted()
returns false
So, in such case I have the question: how to get a permission to get the music from internal storage?
You should be checking for READ_EXTERNAL_STORAGE also
public boolean isReadStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted1");
return true;
} else {
Log.v(TAG,"Permission is revoked1");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted1");
return true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.