HTC Desire上的内部存储

时间:2011-05-25 19:37:14

标签: file data-storage android

我正在尝试将文件保存到内部存储(不是SD卡)。正如文档所指出的那样,如果我使用getExternalStorageDirectory(),数据将被置于/Android/data/<package_name>/下,但那里什么都没有。此代码取自here。但是,类似的代码在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没有错误。任何提示?

2 个答案:

答案 0 :(得分:1)

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

File path = Environment.getExternalStorageDirectory();

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


修改

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();
}

答案 1 :(得分:0)

忘记更新。 当我只是拔下设备并运行应用程序时,问题就消失了。一位朋友告诉我,他遇到了同样的问题并通过关闭任何可能访问SD卡的应用程序解决了这个问题,例如: Windows资源管理器。