-1

I want to write an image file to sd card, but i get an error :Open Failed eacces permission denied.

i am using this code:

File ff = new File("/storage/sdcard1/DCIM/100ANDRO/DSC_0258.JPG");
try{ 
    ff.createNewFile();
} catch (Exception e){ 
    Log.d("error" , "creating");
    e.printStackTrace(); 
}

I have added permission :-

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

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

I am not using android 6.0 , so permission is not required at run time.

The same code works fine when file path changed to internal storage path.

but still getting error.

Ankesh kumar Jaisansaria
  • 1,563
  • 4
  • 26
  • 44

1 Answers1

1

First, few Android devices will have such a path.

Second, you do not have arbitrary read-write access to removable storage on Android 4.4 and higher.

If you want to give users the option of working with removable storage, use the Storage Access Framework APIs on Android 4.4+.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • this path I get using getExternalStorage code. And other than Android 4.4 I want to write to sd card – Ankesh kumar Jaisansaria Apr 06 '16 at 14:52
  • @AnkeshkumarJaisansaria: "this path I get using getExternalStorage code" -- then your question should show this code, not some hardcoded string. We cannot help you with code that is not in the question. "And other than Android 4.4 I want to write to sd card" -- [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) is not [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). There is no official support for removable storage prior to Android 4.4. – CommonsWare Apr 06 '16 at 14:56
  • Thanks for your reply, But then i think how file manager application read sd card and how can they add,rename,delete files. – Ankesh kumar Jaisansaria Apr 06 '16 at 15:03
  • @AnkeshkumarJaisansaria: They use undocumented and unsupported techniques that, as a result, fail to work on some devices. – CommonsWare Apr 06 '16 at 15:09
  • I am now using SAF for Sd Card writting, but getting error. Please help me with SAF. Really stuck at this. I have posted my issue in new question . Link :- http://stackoverflow.com/q/38998276/5309039 – Ankesh kumar Jaisansaria Aug 17 '16 at 13:39