gpt4 book ai didi

android - 从url下载文件并保存到内存磁盘android

转载 作者:太空狗 更新时间:2023-10-29 12:54:09 25 4
gpt4 key购买 nike

我试图从 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);

你应该 check media availability在尝试保存之前。

关于android - 从url下载文件并保存到内存磁盘android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8687391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com