I have added all the required permissions to the manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
And I implemented following in my activity:
if (ContextCompat.checkSelfPermission(LibraryActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(LibraryActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(LibraryActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST);
} else {
ActivityCompat.requestPermissions(LibraryActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST);
}
} else {
doStuff();
}
I also implemented onRequestPermissionsResult method:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSION_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(LibraryActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
doStuff();
}
} else {
Toast.makeText(this, "No permission granted", Toast.LENGTH_SHORT).show();
finish();
}
}
}
And I am triying to play music from internal storage using file:
MediaPlayer player = MediaPlayer.create(getContext(), Uri.fromFile(file));
In android 7 file.canRead() is returning true and the program is working fine, but in android 10 file.canRead() is returning false and the program is not working.