blastervla blastervla - 1 year ago 46
Java Question

Writing files to external storage in Android failing

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

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()];;

// 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);



What should I do?

Answer Source

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.