毕加索图片未在首次运行时加载 [英] Picasso image is not loading on first run
问题描述
我正在使用毕加索从URL加载图像.由于我需要位图进行进一步处理,因此我使用Target()类保存位图.但是毕加索并没有在第一次运行时加载图像.但是它在我进行另一项活动并回到毕加索实现的活动时加载.为什么会这样呢?有任何解决办法吗?我的代码在下面,
I am using picasso to load image from a url. Since i needed bitmap for further processing, I am using Target() class for saving the bitmap. But picasso is not loading the image on the first run. But it loads at the time when i goes to another activity and getting back to the picasso implemented activity. Why it is happening ? Any fixes? My code is below,
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);
}
推荐答案
这是一个已知问题,因为毕加索只保留一周参考:
Its a known issue, as picasso only keeps a week reference:
解决此问题的方法是将目标设置为要设置的视图组件的tag
.
A solution to this issue would be to set the target as a tag
to the view component you wish to set.
因此您的代码将如下所示:
So your code will look like this:
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);
此发布!
这篇关于毕加索图片未在首次运行时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!