简体   繁体   中英

Android - External Memory Card is not writeable from my app

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM