从URL中下载文件并保存到内存中磁盘的android [英] Download a file from url and save to memory disk android
本文介绍了从URL中下载文件并保存到内存中磁盘的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从URL中下载文件并将其保存到内存车,但我不能明白什么是我的错,我的code是
I have tried to download a file from url and save it to memory cart, but I cant understand what is my wrong, my code is
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");
但我发现下面的错误在我的控制台
but i found the following error in my console
12-27 14:50:01.302: D/EXCEPTION GET:(8062): java.io.FileNotFoundException: /my.jpg (Read-only file system)
其实我不知道我能做些什么,任何人的帮助吧。
Actually i dont know what can i do, anyone help please.
推荐答案
问题是与你的路径保存文件,文件名不应该用 /
并启动此修改适用于您的code。
The problem is with your path to save the file , fileName shouldn't start with a /
and apply this modification to your code .
File extStore = Environment.getExternalStorageDirectory();
File file = new File(extStore, fileName);
您应该检查介质的可用性保存。
这篇关于从URL中下载文件并保存到内存中磁盘的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文