【发布时间】:2016-07-23 12:43:46
【问题描述】:
我正在使用毕加索。 我想先将图像添加到位图,然后将其添加到图像视图。我正在使用以下代码行,它使用 uri 从图库中添加图像并将其显示在图像视图上。我想先将它保存在位图上。我该怎么办:
Picasso.with(this).load(uriadress).into(imageView);
但我想先将其保存在位图上。
【问题讨论】:
我正在使用毕加索。 我想先将图像添加到位图,然后将其添加到图像视图。我正在使用以下代码行,它使用 uri 从图库中添加图像并将其显示在图像视图上。我想先将它保存在位图上。我该怎么办:
Picasso.with(this).load(uriadress).into(imageView);
但我想先将其保存在位图上。
【问题讨论】:
Picasso 持有具有弱引用的 Target 实例。
所以最好将Target 作为实例字段。
见:https://stackoverflow.com/a/29274669/5183999
private Target mTarget;
void loadImage(Context context, String url) {
final ImageView imageView = (ImageView) findViewById(R.id.image);
mTarget = new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
//Do something
...
imageView.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(context)
.load(url)
.into(mTarget);
}
【讨论】:
Target 设置为实例字段,Target 将被垃圾回收。更多信息,请看本期github.com/square/picasso/issues/352
你可以这样做
private Target image;
image = 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() + "/" + FILEPATH);
try {
file.createNewFile();
FileOutputStream outstream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 75, outstream);
outstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
Picasso.with(this)
.load(currentUrl)
.into(image);
【讨论】: