在Android中创建新文件时出错:FileNotFoundException:/test.png:open失败:EROFS(只读文件系统)
我在原始文件夹中有一个png文件。我使用以下方法获取inputStream:在Android中创建新文件时出错:FileNotFoundException:/test.png:open失败:EROFS(只读文件系统),android,file,filenotfoundexception,Android,File,Filenotfoundexception,我在原始文件夹中有一个png文件。我使用以下方法获取inputStream: inputStream = getResources().openRawResource(R.raw.test); 我正在尝试在Android应用程序中的新文件中编写此inputStream。这是我的代码: inputStream = getResources().openRawResource(R.raw.test); File file = new
inputStream = getResources().openRawResource(R.raw.test);
我正在尝试在Android应用程序中的新文件中编写此inputStream。这是我的代码:
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)
基本上,我想创建一个文件对象,以便将其发送到服务器。
谢谢。您将无法访问您正试图访问的文件系统根目录。出于您的目的,您可以写入内部文件
新文件(“test.png”)
,将文件放在应用程序内部存储器中——最好使用getFilesDir()
显式访问它
对于真正的临时文件,您可能需要查看getCacheDir()
——如果忘记删除这些临时文件,系统将在空间不足时回收空间。以下是我的解决方案:
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();