我尝试从 url 下载文件并将其保存到内存购物车,但我无法理解我的错误是什么,我的代码是
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)
其实我不知道我能做什么,请大家帮忙。
问题在于您保存文件的路径,文件名不应以/
并将此修改应用到您的代码中。
File extStore = Environment.getExternalStorageDirectory();
File file = new File(extStore, fileName);
你应该检查媒体可用性 http://developer.android.com/guide/topics/data/data-storage.htmC#filesExternal在尝试保存之前。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)