写入SDCard文件无异常失败
我已经经历了有关 SD 卡文件写入问题的多个线程,但找不到对我有帮助的答案。
- 我已经
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在我的节点清单中使用了。
2. 以下代码创建一个文件,但不写入该文件。 Writer 也从不抛出异常。当我返回 SDCard 并看到创建的文件为 0 kb 时。 (这是由于createnew函数)
3. SDCard显示使用这些权限创建的文件----rwxr-x(其他人不存在写入)
4. 该行为在模拟器或 ASUS pad 或 Acer Iconia 设备等设备上是相同的。
应用程序从 Android 应用程序创建包含 SDCard 内容的文件的常规方式是什么?
if(isExternalStorageWritable()) {
File testFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),Filename);
try {
if (testFile.createNewFile()) {
Log.i(TAG + ": createSdcardFile","Empty file on external storage created");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
openFileOutput(Filename, MODE_APPEND)));
out.write(Content);
out.close();
Log.i(TAG + ": createSdcardFile","Content to new file created");
created = true;
}
} catch(IOException e) {
Log.e(TAG,"Unable to create/write to new file on external storage. Terminating testCDMReadPositive");
Log.e(TAG,e.getMessage());
}
}
4.使用模式MODE_WORLD_WRITABLE给出相同的结果,即SDCard上的0kb文件
5. 任何有在外部存储(/mnt/sdcard)上写入文件经验的人都会知道如何做到这一点。任何帮助或指导,高度赞赏。谢谢
I have gone through multiple threads about SDcard file writing problems, but could not see a answer that would help me.
- I have used
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my manifest for node.
2. Following code creates a file but doesn't write to it. Writer never throws an exception either. When I go back to SDCard and see the file created is of 0 kb. ( which is due to createnew function)
3. SDCard shows the file created with these permissions ----rwxr-x ( write not being there for others )
4. The behaviour is same on emulator or devices liek ASUS pad or Acer Iconia devices.
What is the regular way applications create files with content on SDCard from an Android application ?
if(isExternalStorageWritable()) {
File testFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),Filename);
try {
if (testFile.createNewFile()) {
Log.i(TAG + ": createSdcardFile","Empty file on external storage created");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
openFileOutput(Filename, MODE_APPEND)));
out.write(Content);
out.close();
Log.i(TAG + ": createSdcardFile","Content to new file created");
created = true;
}
} catch(IOException e) {
Log.e(TAG,"Unable to create/write to new file on external storage. Terminating testCDMReadPositive");
Log.e(TAG,e.getMessage());
}
}
4.Using mode MODE_WORLD_WRITABLE gives same result i.e. a 0kb file on SDCard
5. Anyone with experience in writing files on externalstorage ( /mnt/sdcard ) would know how to do this. Any help or direction, highly appreciated. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是一个愚蠢的建议,但是您在新运行之前删除了旧测试文件吗?因为如果文件已经存在,您的
if
子句将阻止写入该文件。Maybe a stupid suggestion, but did you delete the old testfile before the new run? Because your
if
clause will prevent writing to the file if it already existed.