我正在尝试使用Picasso目标在适配器的getView()函数中下载和保存图像。我有一个指向图像的网址。此图像保存在手机上。如果图像已经保存,我将从手机内存和显示器中检索它,否则我使用毕加索目标将位图保存到手机的存储器中。我的问题是,目标永远不会进入onBitmapLoaded函数,它只会进入onBitmapFailed。因此,我的位图永远不会保存到手机存储,因为保存发生在onBitMapLoaded函数中。
下面的代码是getView()函数,在try catch块的底部,它检查文件是否已经保存在光盘上。如果是,我尝试将文件解码为Bitmap,并将其显示在imageView中。否则,我使用Picasso Target下载图像。
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
// Get or create the feedback wrapper to allow click feedback
LinearLayoutFeedback feedbackWrapper;
final ImageView imageView;
// First try to reuse previous views if available
if (convertView != null) {
feedbackWrapper = (LinearLayoutFeedback) convertView;
imageView = (ImageView) feedbackWrapper.getChildAt(0);
}
// If not create new views
else {
feedbackWrapper = new LinearLayoutFeedback(viewGroup.getContext());
imageView = new ImageView(viewGroup.getContext());
imageView.setLayoutParams(new ViewGroup.LayoutParams(PHOTOSTREAM_PICTURE_WIDTH, PHOTOSTREAM_PICTURE_HEIGHT));
imageView.setPadding(0, 0, 0, 0);//padding for image tiles
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setBackgroundColor(getResources().colorForClass(R.color.pstream_black));
feedbackWrapper.addView(imageView);
}
int circularPosition = getCircularPosition(position);
Photo photo = getPhotoData().get(circularPosition);
//Below line was commented out, and instead of using an ImageRequest to load into ImageView, we just use Picasso, it's easier and, handles
//errors better
// loadImage(photo.imageMed, imageView);
final String url = photo.imageMed;
try{
String fileName = Environment.getExternalStorageDirectory().getPath() + "/" + url;
File pic = new File(fileName);
if(pic.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(fileName);
imageView.setImageBitmap(bitmap);
}else{
// loadImageWithPicasso(photo.imageMed, imageView);
final Target target = getTarget(url, imageView);
Picasso.with(getContext()).load(url).into(target);
imageView.setTag(target);
}
}catch (Exception e ){
e.printStackTrace();
}
return feedbackWrapper;
}
以下是getTarget(String url,ImageView imageView)
的代码Target getTarget(final String url, final ImageView imageView){
Target target = new Target(){
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
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, 100, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.art_white)); }
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.art_white)); }
};
return target;
}
我不明白为什么getTarget函数永远不会进入onBitmapLoaded。我也不明白为什么使用此代码在imageView中没有显示图像。任何帮助将不胜感激。
答案 0 :(得分:0)
上面的代码有效。原来wifi失败了,所以我从来没有收到过URL的图片......真是太糟糕了,在这样的问题上浪费了这么多时间。代码的唯一部分仍然是错误的是它试图将位图保存到SD卡的部分..仍然试图找出我在那里做错了什么,但是当我这样做时,我会发布我的解决方案。