【发布时间】:2014-09-01 03:55:15
【问题描述】:
我有一个加载图像的方法,如果图像尚未加载,它将在服务器上查找它。然后它将它存储在应用程序文件系统中。如果它在文件系统中,它会加载该图像,因为这比从服务器中提取图像要快得多。如果您之前加载了图像而没有关闭应用程序,它将存储在静态字典中,以便可以在不占用更多内存的情况下重新加载,以避免内存不足的错误。
这一切都很好,直到我开始使用 Picasso 图像加载库。现在我正在将图像加载到 ImageView 中,但我不知道如何获取返回的位图,以便将其存储在文件或静态字典中。这让事情变得更加困难。因为这意味着它每次都尝试从服务器加载图像,这是我不希望发生的事情。有没有办法在将位图加载到 ImageView 后获取位图?以下是我的代码:
public Drawable loadImageFromWebOperations(String url,
final String imagePath, ImageView theView, Picasso picasso) {
try {
if (Global.couponBitmaps.get(imagePath) != null) {
scaledHeight = Global.couponBitmaps.get(imagePath).getHeight();
return new BitmapDrawable(getResources(),
Global.couponBitmaps.get(imagePath));
}
File f = new File(getBaseContext().getFilesDir().getPath()
.toString()
+ "/" + imagePath + ".png");
if (f.exists()) {
picasso.load(f).into(theView);
下面这行是我尝试检索位图时抛出空指针异常,我认为这是因为毕加索需要一段时间才能将图像添加到 ImageView
Bitmap bitmap = ((BitmapDrawable)theView.getDrawable()).getBitmap();
Global.couponBitmaps.put(imagePath, bitmap);
return null;
} else {
picasso.load(url).into(theView);
return null;
}
} catch (OutOfMemoryError e) {
Log.d("Error", "Out of Memory Exception");
e.printStackTrace();
return getResources().getDrawable(R.drawable.default1);
} catch (NullPointerException e) {
Log.d("Error", "Null Pointer Exception");
e.printStackTrace();
return getResources().getDrawable(R.drawable.default1);
}
}
任何帮助将不胜感激,谢谢!
【问题讨论】:
-
毕加索不会每次都从服务器加载图像。只有在第一次然后它从缓存目录加载。
标签: android bitmap imageview picasso