Unable to write files on sdcard android

时间秒杀一切 提交于 2019-11-27 18:42:13

问题


I have an app which saves backup files on SD-Card. It works fine on HTC Nexus One, and other android phones, but with some 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?


回答1:


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.




回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/6311852/unable-to-write-files-on-sdcard-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!