简体   繁体   English

无法在 sdcard android 上写入文件

[英]Unable to write files on sdcard android

I have an app which saves backup files on SD-Card.我有一个将备份文件保存在 SD 卡上的应用程序。 It works fine on HTC Nexus One, and other android phones, but with some phones it doesn't work (reading or writing).它在 HTC Nexus One 和其他 android 手机上运行良好,但在某些手机上它不起作用(读取或写入)。

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?如何获得 SD 卡的正确路径以正确处理文件?

Generally, Android wouldn't allow you to write a file directly to the SDCard's root.通常,Android 不允许您将文件直接写入 SDCard 的根目录。 You must create a folder in the SDCard root, and then write your file inside the newly created folder.您必须在 SDCard 根目录中创建一个文件夹,然后将文件写入新创建的文件夹中。

Try this, and it should work.试试这个,它应该可以工作。

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?你的意思是程序运行没有投诉,但文件没有被写入 sdcard,还是你的意思是别的?

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.我见过一些设备从 getExternalStorageDirectory() 返回“/mnt/sdcard”,但这并不是真正的 sdcard 路径,例如,在至少一台摩托罗拉设备和三星 Galaxy Tab (7") 上,即使这指向内部存储,也会返回 /mnt/sdcard。每个设备上的外部存储是 /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.如果您想要解决方法,您可以阅读 /proc/mounts 并尝试找到 sdcard 的实际路径,但 /mnt/sdcard 也会显示在那里,并且没有保证区分真相的方法所有设备。

I have Sony Xperia tipo dual and this:我有 Sony Xperia tipo dual 和这个:

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) (我是在 Native 中完成的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM