Skip to content Skip to sidebar Skip to footer

Why Can't I Write To Android External Storage?

I am trying to write an Android App that, among other things, needs to read and write files to 'external' storage. While I am able to browse and select a folder on external storage

Solution 1:

Apparently, Android 10 is to blame.

I lowered the Target SDK to 28 (Android 9) and the code works.

It looks like if I want this to work for Android 10, I will have to use MediaStore for SDK 29+.

Solution 2:

you have to check for runtime permission before writeFileExternalStorage function :

private static final int WRITE_EXTERNAL_STORAGE = 0; private static final int REQUEST_PERMISSION = 0;

int permissionCheckStorage = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheckStorage != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_PERMISSION);

        return;
    }

Post a Comment for "Why Can't I Write To Android External Storage?"