您无法获取位图参考,因为它从未保存在 ImageView 中。但是你可以使用:
((BitmapDrawable)this.getDrawable()).getBitmap();
因为当你用 Volley 设置它时,你这样做:
/**
* Sets a Bitmap as the content of this ImageView.
*
* @param bm The bitmap to set
*/
@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
但是,如果您以任何其他方式设置默认图像或错误图像或任何其他图像,您可能无法获得 BitmapDrawable 但例如 NinePatchDrawable。
以下是如何检查:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}