gpt4 book ai didi

Android:从 url 下载大图像文件(最多 2 mb)

转载 作者:搜寻专家 更新时间:2023-11-01 09:01:43 25 4
gpt4 key购买 nike

我想从 url 下载大图像文件,但是当我尝试解码输入流时,它抛出内存不足错误。图像文件大小约为 2mb。

如何下​​载大图文件并在图片 View 中显示?

最佳答案

看看这个 Download a file with Android, and showing the progress in a ProgressDialog

try {
URL url = new URL("image download url");
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();

// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/image_name.extension");

byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;

最好将其与 Asynctask 或线程一起使用

关于Android:从 url 下载大图像文件(最多 2 mb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14602457/

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