0

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.

Javlon
  • 1,108
  • 2
  • 15
  • 29
  • You do not have access to arbitrary locations on external storage on Android 10. If all you need is read access, add `android:requestLegacyExternalStorage="true"` to your `` in the manifest. On Android R, [`READ_EXTERNAL_STORAGE` seems to work normally again](https://commonsware.com/blog/2020/03/22/r-raw-paths-all-files-access.html), at least in the second developer preview. – CommonsWare Apr 17 '20 at 22:13
  • Yes this has worked !!! . Thanks for your answer @CommonsWare. – Javlon Apr 17 '20 at 23:15

0 Answers0