我正在尝试使用API毕加索保存图像.要做到这一点,我正在尝试使用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; }
例外
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; }