HTC Desire 上的内部存储

发布于 2024-11-09 10:43:51 字数 949 浏览 3 评论 0原文

我正在尝试将文件保存到内部存储(不是 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_STORAGEis already added in the manifest. No error in logcat. Any hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-11-16 10:43:51

您混淆了内部和外部存储。您正在谈论内部存储,而实际上您正在使用外部存储:

File path = Environment.getExternalStorageDirectory();

生成的文件将位于此处:/mnt/sdcard/DemoPicture.jpg。但如果您确实想使用内部存储,请查看 Context.openFileOutput 函数。


编辑:

文件路径=Environment.getExternalStorageDirectory();
文件 file = new File(路径, "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) {
    Toast.makeText(this, "Failed to write a file", Toast.LENGTH_LONG).show();
}

You're confusing Internal and External storages. You are talking about internal storage, while actually you are using external storage:

File path = Environment.getExternalStorageDirectory();

The resulting file will be located here: /mnt/sdcard/DemoPicture.jpg. But if your really want to use internal storage, take a look at Context.openFileOutput function.


EDIT:

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) {
    Toast.makeText(this, "Failed to write a file", Toast.LENGTH_LONG).show();
}
暗藏城府 2024-11-16 10:43:51

忘记更新了。
当我简单地拔掉设备并运行应用程序时,问题就消失了。一位朋友告诉我,他也遇到了同样的问题,并通过关闭任何可能访问 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文