Solve the problem that android 6.0 or above cannot read external storage permissions
- 2021-10-11 19:37:01
- OfStack
When the local video of mobile phone sdcard is obtained through the content provider,
/**
* From local sdcard Get data
* //1. Traversal sdcard, Suffix name
* //2. Get video from content provider
* //3. If it is 6.0 The system of dynamic acquisition and reading sdcard Permissions of
*/
private void getDataFromLocal() {
new Thread(){
@Override
public void run() {
super.run();
// isGrantExternalRW((Activity) context);
// SystemClock.sleep(2000);
mediaItems = new ArrayList<>();
ContentResolver resolver = context.getContentResolver();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] objs = {
MediaStore.Video.Media.DISPLAY_NAME,// Video files are stored in the sdcard Name of
MediaStore.Video.Media.DURATION,// Total video duration
MediaStore.Video.Media.SIZE,// File size of video
MediaStore.Video.Media.DATA,// Absolute address of video
MediaStore.Video.Media.ARTIST,// A singer of a song
};
Cursor cursor = resolver.query(uri, objs, null, null, null);
if(cursor != null){
while (cursor.moveToNext()){
MediaItem mediaItem = new MediaItem();
mediaItems.add(mediaItem);// Write it on it
String name = cursor.getString(0);// Name of video
mediaItem.setName(name);
long duration = cursor.getLong(1);// Duration of video
mediaItem.setDuration(duration);
long size = cursor.getLong(2);// File size of video
mediaItem.setSize(size);
String data = cursor.getString(3);// Play address of video
mediaItem.setData(data);
String artist = cursor.getString(4);// Artist
mediaItem.setArtist(artist);
}
cursor.close();
}
//Handler Send a message
handler.sendEmptyMessage(10);
}
}.start();
}
/**
* Solve Android 6.0 The above version cannot read the external storage permissions
* @param activity
* @return
*/
public static boolean isGrantExternalRW(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1);
return false;
}
return true;
}