gpt4 book ai didi

android - 使用 picasso 从网址保存图像?

转载 作者:IT老高 更新时间:2023-10-28 21:59:14 24 4
gpt4 key购买 nike

我正在尝试使用 API Picasso 保存图像。为此,我正在尝试使用 Target 进行保存,但我无法完成这项工作。

我该怎么做?

尝试

//save image
public static void imageDownload(Context ctx){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png"));
}

//target to save
private static Target getTarget(final String url){
Target target = new Target(){

@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
//Log.i("PRODUTOS_FOLDER", CreateAppFolder.getProdutosFolder());
File file = new File(Environment.getExternalStorageDirectory() + url);

try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();

}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};
return target;
}

异常(exception)

java.io.IOException: open failed: ENOENT (No such file or directory)

最佳答案

解决了。现在工作正常!

我做到了

//save image
public static void imageDownload(Context ctx, String url){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget(url));
}

//target to save
private static Target getTarget(final String url){
Target target = new Target(){

@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {

@Override
public void run() {

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();

}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};
return target;
}

关于android - 使用 picasso 从网址保存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32799353/

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