三星 Nexus S android 4.0:无法在 sdcard 上的目录中创建文件
我有一台加载了 android 4.0 的 Samsung Nexus S 设备。我试图在 SD 卡上的现有文件夹中创建一个文件并收到“权限被拒绝”。在以下代码中,exists() 返回 true,但 canWrite() 返回 false。为什么?
File exst = Environment.getExternalStorageDirectory();
String exstPath = exst.getPath();
File d = new File(exstPath+"/TestDir/");
if (!d.exists())
{
int b = 1;
}
if (!d.canWrite())
{
int a = 1;
}
顺便说一句,我已将
添加到清单中,但这没有帮助。
I have a Samsung Nexus S device with android 4.0 loaded on it. I am trying to create a file in an existing folder on sdcard and get a "permission denied". In the following code, exists() returns true but canWrite() returns false. Why?
File exst = Environment.getExternalStorageDirectory();
String exstPath = exst.getPath();
File d = new File(exstPath+"/TestDir/");
if (!d.exists())
{
int b = 1;
}
if (!d.canWrite())
{
int a = 1;
}
By the way, I've added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to the manifest but that did not help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的手机是否已插入电脑?如果是这样,计算机将控制 SD 卡并不允许对其进行写入。如果是这种情况,请尝试将连接模式更改为“仅充电”。
Is your phone plugged into your computer? If so, the computer will take control of the SD card and not allow it to be written to. Try changing the connection mode to 'Charge Only' if this is the case.
将 getAbsolutePath() 附加到您的第一行,然后它应该可以工作:
更新:
查看我自己的代码和其他 SO 答案,我相信您不会使用 canWrite 来检查路径是否在 SD 卡上可写。相反,您使用Environment.MEDIA_MOUNTED:
Append getAbsolutePath() to your first line, then it should work:
Update:
Reviewing my own code and other SO answers, I believe you do not use canWrite to check if a path is writable on a SD card. Instead you use Environment.MEDIA_MOUNTED:
的非标准实现
API “/external_sd/”
尽管所有答案都给出了部分建议,但问题很可能是三星对文件名之前的路径的
。以下是三星对其“破坏” API 的解释
http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=162934&messageNumber=1381&startId=zzzzz~&searchType=TITLE&searchText= sdcard
在几个 SO 帖子中也提到了它,但我没有方便的链接......
[编辑中May, 2013] 这是这个问题的病理:您可以通过提到的各种常规方法获取path()。然后,只需编写一个简单的文件并观察它显示在 DDMS 文件资源管理器中。尝试硬编码确切的路径和文件名或使用 /external_sd/ 东西 - 在这两种情况下,您将无法读回自己的文件! (或者你可以,但它会包含垃圾。)我们已经在三款不同的 S3 手机上验证了这一点。将通过其 RemoteTestingLab 网站在三星的“真实”手机上进行更多测试并报告。
Although all the answers give partial suggestions, the problem is very likely Samsungs non-standard implementation of the API
"/external_sd/"
to the path before the file name.
Here's Samsungs explanation for their "breaking" of the API
http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=162934&messageNumber=1381&startId=zzzzz~&searchType=TITLE&searchText=sdcard
it's also mentioned in several SO posts, but I don't have the links handy...
[Edit Mid May, 2013] Here's the pathology of this problem: you can get the path() by the various normal methods mentioned. Then, just write a simple file and watch it show up in the DDMS file explorer. Try hardcoding that exact path and file name OR use the /external_sd/ thing - in both cases, you will not be able to read your own file back in ! (Or you might, but it will contain garbage.) We've verified this on three different S3 phones. Will test more on Samsungs "real" phones via their RemoteTestingLab site and report back.