Unable to write files on sdcard android
I have an app which saves backup files on SD-Card. It works fine on HTC Nexus One, and other android phones, but with so开发者_高级运维me phones it doesn't work (reading or writing).
In the manifest I have set this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For example (when I set the path for recording file) :
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()
+ "/mydata.dat");
//Writing file...(It doesn't work)
How I can get the right path of the SD-Card for manipulating files rightly?
When you say "it doesn't work".... what exactly happens? Do you mean the program runs without complaint but the file doesn't get written to the sdcard, or do you mean something else?
If the file is simply not getting there, it's not your fault, it's the phone manufacturer's fault. I've seen several devices that return "/mnt/sdcard" from getExternalStorageDirectory(), however this is not truly the path to the sdcard! On at least one Motorola device and on the Samsung Galaxy Tab (7"), for example, /mnt/sdcard is returned even though this points to internal storage, and the external storage on each of these devices is /mnt/sdcard-ext.
There's nothing legitimately you can do about this -- the OEM is lying to you. If you want a hack of a work-around, you can read /proc/mounts and try to find the actual path to the sdcard, but /mnt/sdcard will also show up there, and there's no guaranteed way to distinguish the truth for all devices.
Generally, Android wouldn't allow you to write a file directly to the SDCard's root. You must create a folder in the SDCard root, and then write your file inside the newly created folder.
Try this, and it should work.
I have 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 ok. (I did it in Native)
精彩评论