我正在尝试从 android 应用程序中的 Web URL 保存图像,但是当我运行它时,日志猫会抛出一个异常,说它是“只读的”。我不知道发生了什么事。
这是我的下载器类:
public class ImageDownload {
public static void downloader(String imageURL, String fileName) {
try {
URL url = new URL("http://www.exampleurl.com/" + imageURL);
File file = new File(fileName);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d("Downloader", "Error: " + e);
}
}
}
当我运行它时,这是我从 logcat 得到的:
DEBUG/Downloader(22112): Error: java.io.FileNotFoundException: /example.gif (Read-only file system)
任何帮助都会很棒。谢谢你。