Android - External Memory Card is not writeable from my app

2k views Asked by At

I need help in getting the attached memory card writable from my app. The android version I'm using is Marshmellow. I requested for runtime permission using the below code.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
            (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
    } 

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_STORAGE) {
        if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
            Log.d(TAG, "Permission Granted");
        }
    }
}

I also checked the settings and the write permission is granted. Below are the two storage locations I have for the phone

/storage/emulated/0 (Internal Memory, Writable)
/storage/9C33-6BBD (SDCard, Not writable, only readable)

I tried the below code just to check if my memory card is writable

File file = new File("/storage/9C33-6BBD");
file.canWrite() // returns false every time

I'm not sure what I'm missing or doing wrong, other apps seem to have no problem in writing to memory card.

I also used adb shell and was able to create a folder in my memory card.

I have also added the below permission in my Manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Please let me know if you need more details. And thanks in advance for helping me out.

1

There are 1 answers

0
CommonsWare On

I'm not sure what I'm missing or doing wrong

You do not have read/write access to arbitrary locations on removable storage on Android 4.4+.

other apps seem to have no problem in writing to memory card.

Those apps were pre-installed by the device manufacturer or custom ROM developer.

Or, those apps are using getExternalFilesDirs(), getExternalCacheDirs(), or getExternalMediaDirs(). If those methods return 2+ locations, the second and subsequent ones are on removable storage, and you can read/write to those locations.

Or, those apps are using ACTION_OPEN_DOCUMENT, ACTION_CREATE_DOCUMENT, or ACTION_OPEN_DOCUMENT_TREE, from the Storage Access Framework.

Or, those apps are exploiting some security flaw that, hopefully, will get fixed.