0

在我的项目中,我使用(Volley + NetworkImageView)下载了一些图像和文本并在列表视图中显示它们......直到这里,我没有任何问题。

现在,我想从 NetworkImageView 中获取位图,我尝试了以下许多方法,但没有一个对我有用。

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

另一种方法:

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();

他们都没有工作..

任何帮助表示赞赏,,

4

1 回答 1

1

您无法获取位图参考,因为它从未保存在 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
    }
于 2016-04-03T01:55:15.693 回答