HTC Desire 上的内部存储
我正在尝试将文件保存到内部存储(不是 SD 卡)。正如文档指出的,如果我使用 getExternalStorageDirectory()
,数据将被放置在 /Android/data/
下,但那里什么也没有。此代码取自此处< /a>.不过,类似的代码也适用于 Archos。
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "DemoPicture.jpg");
txtList.append(file.getPath());
try {
path.mkdirs();
InputStream is = getResources().openRawResource(R.drawable.icon);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
Log.d("ExternalStorage", "Error writing " + file, e);
}
我使用Android SDK 2.1。权限WRITE_EXTERNAL_STORAGE
已添加到清单中。 logcat 中没有错误。有什么提示吗?
I'm trying to save a file to internal storage (not SD Card). As the documentation points out, if I use getExternalStorageDirectory()
, the data will be placed under /Android/data/<package_name>/
, but there's nothing there. This code is taken from here. Similar code worked on Archos, though.
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "DemoPicture.jpg");
txtList.append(file.getPath());
try {
path.mkdirs();
InputStream is = getResources().openRawResource(R.drawable.icon);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
Log.d("ExternalStorage", "Error writing " + file, e);
}
I use Android SDK 2.1. Permission WRITE_EXTERNAL_STORAGE
is already added in the manifest. No error in logcat. Any hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您混淆了内部和外部存储。您正在谈论内部存储,而实际上您正在使用外部存储:
生成的文件将位于此处:
/mnt/sdcard/DemoPicture.jpg
。但如果您确实想使用内部存储,请查看Context.openFileOutput
函数。编辑:
文件路径=Environment.getExternalStorageDirectory();
文件 file = new File(路径, "DemoPicture.jpg");
txtList.append(file.getPath());
You're confusing Internal and External storages. You are talking about internal storage, while actually you are using external storage:
The resulting file will be located here:
/mnt/sdcard/DemoPicture.jpg
. But if your really want to use internal storage, take a look atContext.openFileOutput
function.EDIT:
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "DemoPicture.jpg");
txtList.append(file.getPath());
忘记更新了。
当我简单地拔掉设备并运行应用程序时,问题就消失了。一位朋友告诉我,他也遇到了同样的问题,并通过关闭任何可能访问 SD 卡的应用程序(例如 Windows 资源管理器)来解决该问题。
Forgot to update.
The problem disappears when I simply unplug the device and run the app. A friend told me he got the same problem and solved it by closing any apps that are likely to access the SD card, e.g. Windows Explorer.