我在raw文件夹中有一个png文件。我使用:
获取inputStreaminputStream = getResources().openRawResource(R.raw.test);
我正在尝试将此inputStream写入Android应用程序的新文件中。这是我的代码:
inputStream = getResources().openRawResource(R.raw.test);
File file = new File("/test.png");
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024*1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
inputStream.close();
当我运行应用程序时,我在logcat中收到以下错误:
java.io.FileNotFoundException: /test.png: open failed: EROFS (Read-only file system)
基本上我想创建一个File对象,以便我可以将它发送到我的服务器。 谢谢。
答案 0 :(得分:0)
您将无权访问您尝试访问的文件系统根目录。出于您的目的,您可以写入内部文件new File("test.png")
,将文件放在应用程序内部存储中 - 更好,使用getFilesDir()
显式访问它。
对于真正的临时文件,您可能需要查看getCacheDir()
- 如果您忘记删除这些临时文件,系统将在空间不足时回收空间。
答案 1 :(得分:0)
这是我的解决方案:
inputStream = getResources().openRawResource(R.raw.earth);
file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
file.createNewFile();
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024*1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
inputStream.close();