9

As the title points out, I'm having trouble writing files to the external storage. My debug device is a Nexus 5. The thing is, I'm able to read files perfectly from the device (I've been trying with the ones in the Download Folder) but cannot write them. I am aware that I must do this while the device isn't connected to the computer. But it doesn't work either.

In fact, I've tried reading the state of the SD card prior to writing to it (which didn't work, of course). The state showed as "mounted" either when the device was connected to my PC or not. And I compared the state to Environment.MEDIA_MOUNTED_READ_ONLY and Environment.MEDIA_MOUNTED without any success. My device is in none of these states.

One thing which you must know is that my phone doesn't have an external SD card, as it's an internal one. This results in my device having a "/storage/emulated/0/..." directory for the external storage.

I must also point out that I have implemented the following tags in my Android Manifest:

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

I don't have any clue to what might be happening. Another thing which might help is that I've tried managing files with winrar (for Android) and I've been able to remove files with the device connected to my PC as well as without having it connected. So I don't know what to do.

The code which I'm using to write a file is the following. Bear in mind that it should read an image file (which it does), convert it into a string, convert it back into an image and then save it to the Downloads Folder:

 File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/base_image.jpg");
 // Reading a Image file from file system
 FileInputStream imageInFile = new FileInputStream(file);
 byte imageData[] = new byte[(int) file.length()];
 imageInFile.read(imageData);

 // Converting Image byte array into Base64 String
 String imageDataString = encodeImage(imageData);

 // Converting a Base64 String into Image byte array
 byte[] imageByteArray = decodeImage(imageDataString);

 File newFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "converted_image.jpg");

 //Write a image byte array into file system
 FileOutputStream imageOutFile = new FileOutputStream(newFile);

 imageOutFile.write(imageByteArray);

 imageInFile.close();
 imageOutFile.close();

What should I do?

Universal Electricity
  • 775
  • 1
  • 12
  • 26
blastervla
  • 539
  • 6
  • 19
  • Are you sure the file exits to write to it? – Raghunandan Jul 30 '15 at 03:15
  • I'm creating a new file. Or, at least, that's what I'm attempting. I get the File Not Found Exception while trying to create it and the EACCES (Permission denied) error. That's my main problem here. Am I doing something wrong? – blastervla Jul 30 '15 at 03:20

2 Answers2

5

Just fix ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE to android.permission.WRITE_EXTERNAL_STORAGE in your uses-permission.

I've encounterd this problem, UPPERCASE in permission is not useful.

Desmond Yao
  • 545
  • 1
  • 4
  • 10
  • 1
    I cannot possibly thank you enough for your answer. This has been bothering me for days. Thank you very very much!! – blastervla Jul 30 '15 at 04:02
3

FileOutputStream does NOT automatically create a file if it's not exist.

So, you need to check and create if your file doesn't exist.

if(!newFile.exists()) {
    newFile.createNewFile();
} 

Hope this help!

Banana droid
  • 690
  • 1
  • 9
  • 27
  • Thanks for your fast response. I tried it, but it now throws me the following exception: "java.io.IOException: open failed: EACCES (Permission denied)" when I call the 'newFile.createNewFile();' method. Any ideas to what could be happening? – blastervla Jul 30 '15 at 03:26
  • 1
    Also before calling createFile() call this `newFile.getParentFile().mkdirs()`. P.s. sorry if its mispelt, im going off the top of my head :) – vedi0boy Jul 30 '15 at 03:26
  • Ok well u obviously didnt give your app the proper permissions, can i see where in the manifest that u added the permissions. Edit: Removed last statement, it was wrong. – vedi0boy Jul 30 '15 at 03:31
  • 2
    I'm not sure if `ANDROID.PERMISSION` are accepted. Please change it to `android.permission` and test – Banana droid Jul 30 '15 at 03:35
  • 1
    Also make sure it is declared outside of the tag. Edit: I found this, it has many solutions: http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android – vedi0boy Jul 30 '15 at 03:38