我正在使用毕加索从网址加载图片。由于我需要位图进行进一步处理,我使用Target()类来保存位图。但是毕加索没有在第一次运行时加载图像。但是当我去另一个活动并回到毕加索实施的活动时,它会加载。为什么会这样?任何修复?我的代码如下,
Picasso.with(getActivity()).load(card.getExtras().getImageUrl()).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date now = new Date();
filename ="certificate_"+ formatter.format(now) + ".png";
File path=null;
if (getActivity().getExternalCacheDir()==null) {
path=getActivity().getCacheDir();
}
if(getActivity().getExternalCacheDir()!=null){
path=getActivity().getExternalCacheDir();
}
File image=new File(path+filename);
FileOutputStream fileOutPutStream = null;
try {
fileOutPutStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream);
fileOutPutStream.flush();
fileOutPutStream.close();
Log.d("---REACHED","FILE SAVED--------------");
} catch (Exception e) {
Crashlytics.logException(e);
}
这是一个众所周知的问题,因为毕加索只保留一周参考:
此问题的解决方案是将目标设置为您要设置的视图组件的tag
。
所以你的代码看起来像这样:
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
.....
// set the tag to the view
holder.imageView.setTag(target);
//set the target to picasso
Picasso.with(getActivity()).load(card.getExtras().getImageUrl()).into(target);
在this SO帖子中给出了相同的正确解释!
您可以使用它来加载图像。
Picasso.with(getActivity())。load(car Image Url).into(carIng);
其中,carImg是XML中Imageview的id,carImageUrl是一种资源
试试我使用的这个功能:并使用img.setTag(/*some other object than path of file or errId. But don't forget to add it before using this function*/)
。如果你不想以同样的方式使用它,那么在检查if
时删除getTag()
条件。
public static void setImage(final Context context, final ImageView img, @DrawableRes final int defId,
@DrawableRes final int errId, final File file, Picasso.Priority priority) {
if (null != img.getTag()) {
if (null == img.getDrawable() || !(img.getTag() instanceof String && (img.getTag().equals(file.getAbsolutePath())))) {
try {
if (file.exists()) {
Picasso.with(context.getApplicationContext())
.load(file)
.priority(priority)
.placeholder(defId)
.error(errId)
.fit()
.centerInside()
.tag(context)
.noFade()
.into(img, new Callback() {
@Override
public void onSuccess() {
img.setTag(file.getAbsolutePath());
}
@Override
public void onError() {
img.setTag(errId);
}
});
} else {
img.setImageResource(defId);
img.setTag(defId);
}
} catch (Exception e) {
img.setImageResource(defId);
img.setTag(defId);
}
}
} else {
img.setImageResource(defId);
img.setTag(defId);
}
}
毕加索图像载荷为ViewGroup (RelativeLayout, LinearLayout, FrameLayout
等
在我的情况下跟随方式工作。
需要HandlerThread
和Handler
来加载图像。
以下是kotlin的例子。您可以根据需要使用Java进行转换。
val handlerThread = HandlerThread("ImageLoader")
handlerThread.start()
val handler = Handler(handlerThread.looper)
handler.post({
var bitmap: Bitmap? = null
try {
bitmap = Picasso.with(this).load(iamgeUrl).get()
} catch (e: IOException) {
e.printStackTrace()
} finally {
if (bitmap != null) {
runOnUiThread({
imageView.background = BitmapDrawable(resources, bitmap)
})
}
}
})
希望这会对你有所帮助。
您可以尝试将占位符属性添加到picasso:
Picasso.with(this).load(imageData)
.placeholder(R.drawable.placeholder)
.resize(200,200)
.into(mImageView)
希望对你有所帮助!