0

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 ?

Lowrider
  • 31
  • 1
  • 5

2 Answers2

0

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+.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So the only acces to removable storage has Google itself and the phone manufactuers... great. Anyway thanks for the help. – Lowrider Oct 29 '16 at 20:30
  • @Lowrider: No, the only *arbitrary filesystem* access to removable storage is for Google itself and device manufacturers. You are welcome to use methods like `getExternalFilesDirs()` (note the plural) to get at *Android-supplied* locations on removable storage that you can work with using normal file I/O. Or, use [the Storage Access Framework](https://developer.android.com/guide/topics/providers/document-provider.html) to allow the user to choose where to store things, using the `Uri` you get back to open streams for reading and writing. – CommonsWare Oct 29 '16 at 20:42
-1

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.

This 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.

Community
  • 1
  • 1
FadedCoder
  • 1,517
  • 1
  • 16
  • 36
  • This technique does not work as of at least Android 5.0. This code -- once the missing pieces are filled in -- fails with an exception due to the lack of `WRITE_MEDIA_STORAGE` permission. That permission is `signature|privileged`, which ordinary Android SDK apps cannot hold. – CommonsWare Oct 29 '16 at 19:54