我曾尝试从网址下载文件并将其保存到内存购物车,但我无法理解我的错误,我的代码是
URL url = new URL(imageURL);
File file = new File(fileName);
long startTime = System.currentTimeMillis();
Log.d("ImageManager", "download begining");
Log.d("ImageManager", "download url:" + url);
Log.d("ImageManager", "downloaded file name:" + fileName);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
但我在控制台中发现了以下错误
12-27 14:50:01.302: D/EXCEPTION GET:(8062): java.io.FileNotFoundException: /my.jpg (Read-only file system)
实际上我不知道我能做什么,任何人都可以帮忙。
答案 0 :(得分:3)
问题在于保存文件的路径,fileName不应以/
开头,并将此修改应用于您的代码。
File extStore = Environment.getExternalStorageDirectory();
File file = new File(extStore, fileName);
在尝试保存之前,您应该check media availability。