我在嘗試使用API Picasso保存圖像。要做到這一點,我試圖用Target
來保存,但我無法完成這項工作。使用Picasso從url保存圖像?
我該怎麼做?
試圖
//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;
}
異常
java.io.IOException: open failed: ENOENT (No such file or directory)
你的問題究竟是什麼,回調不起作用,或者你的位圖沒有保存,或者其他什麼? –
@VasylGlodan拋出異常'java.io.IOException:打開失敗:ENOENT(沒有這樣的文件或目錄)' – FernandoPaiva
嗯,我不確定,但你的文件的路徑應該看起來像這樣'/ storage/emulated/0/http:// blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png',系統嘗試建立一個名爲'/storage/emulated/0/http://blog.concretesolutions的目錄。 com.br/wp-content/uploads/2015/04 /',但沒有這樣的目錄。嘗試從文件名中刪除所有特殊字符。 –