gpt4 book ai didi

android - 如何通过URL下载图片到内存?

转载 作者:行者123 更新时间:2023-11-29 23:21:07 25 4
gpt4 key购买 nike

我想下载我已上传到 Firebase 的图片。我得到了 URL 并且能够将它们直接加载到我想要的 ImageView。但我想将它们下载到我在内部存储中的所需路径

我用过 Picasso 但它对我不起作用,因为我在使用 ( new Target() ) 时收到下面的错误 这是我将 Picasso 与 Target 一起使用时遇到的错误

enter image description here

下面是 Picasso 的代码

Picasso.get()
.load(downloadUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/yourDirectory");

if (!myDir.exists()) {
myDir.mkdirs();
}

String name = new Date().toString() + ".jpg";
myDir = new File(myDir, name);
FileOutputStream out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

out.flush();
out.close();
} catch(Exception e){
// some action
}
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

//Some Action;
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//Some Action;
}
}
);

我已经在文档中进行了搜索,但没有成功。请尽快让我知道。如果有人有正确答案。提前致谢

最佳答案

首先:实现Taget类方法清除错误

第二个:您的代码抛出此异常java.io.FileNotFoundException因为 Date 对象的默认格式是 Mon Jan 21 12:10:23 GMT+02:00 2019并且您不能创建名称包含 : 冒号

的文件

第三:检查是否在androidManiefest.xml中添加了android.permission.WRITE_EXTERNAL_STORAGE权限并且需要在marshmallow中使用runtime权限

关于android - 如何通过URL下载图片到内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286813/

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