Failed to write files to android sdcard

I have an application that saves backup files on an SD card. It works great on HTC Nexus One and other Android phones, but it doesnโ€™t work on some phones (read or write).

In the manifest, I set this:

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

For example (when I set the path for the recording file):

 OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/mydata.dat"); //Writing file...(It doesn't work) 

How can I get the correct path to the sd card to manage files properly?

+6
source share
3 answers

When you say "this does not work" ... what exactly is happening? You mean that the program works without complaint, but the file is not written to the SD card, or do you mean something else?

If the file just doesnโ€™t get there, this is not your mistake, this is the error of the phone manufacturer. I have seen several devices that return "/ mnt / sdcard" from getExternalStorageDirectory (), however this is not the true path to the SDK! For example, at least one Motorola device and on the Galaxy Tab (7 ") tab, for example, / mnt / sdcard, even if this indicates internal storage, and the external storage on each of these devices is / mnt / sdcard-ext.

There is nothing legal you can do about this - OEM is lying to you. If you want to hack the work, you can read / proc / mounts and try to find the actual path to the SD card, but / mnt / sdcard will also appear there, and there is no guaranteed way to distinguish true for all devices.

0
source

As a rule, Android will not allow you to write the file directly to the root of the SDCard. You must create a folder in the root directory of the SDCard, and then write the file to the newly created folder.

Try this and it should work.

0
source

I have a Sony Xperia tipo dual and this:

FILE* pFile = fopen("/mnt/sdcard/mydata.dat","w+");

and this:

FILE* pFile = fopen("/sdcard/mydata.dat","w+");

Both work fine. (I did it at Native)

-1
source

Source: https://habr.com/ru/post/890255/


All Articles