简体   繁体   中英

saving file on sdcard with permission error

After countless searching I've managed to find path to my sdcard not the android emulated storage. But when I try to make .txt folder there it ends up with error

/storage/37F0-1515/DCIM/100MEDIA/test.txt: open failed: EACCES (Permission denied)

I don't know why because I have permissions for

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

and also I've enabled the permissions with

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

Here is the code that I'm using

    File sdCard = new File("/storage/37F0-1515/DCIM/100MEDIA");
    File dir = new File(sdCard.getAbsolutePath());


    if (!dir.exists()) {
        dir.mkdirs();
    }

    final File file = new File(dir, "test" + ".txt");
    try {
        BufferedWriter bufferedWriter = null;
        bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write("test");
        bufferedWriter.close();
    }
    catch(Exception e){Log.v("myApp", e.toString());}

I don't know why android won't let me write to sdcard. Do I need some other permissions ?

I don't know why because I have permissions for

Those permissions are for external storage , not removable storage .

I don't know why android won't let me write to sdcard

You do not have arbitrary filesystem access to removable storage on Android 4.4+.

Try using these methods-

  1. Check if the uses-permission statements are in the right place, they should be below <manifest> and above <application> tags. The correct format -

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

  2. Starting from Android Kitkat, there is a new storage policy -

The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.

T his can however be exploited without rooting(although I have not tried this one), as mentioned here

EDIT -

The above mentioned exploit won't work for Android 5+. There is no standard solution to this problem. Some exploits may be available but they will be device-specific/not reliable/root-access dependent. However, tools like the Storage Access Framework can be used.

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