如何获取ImageView中的当前drawable并将其转为位图?

How to get the current drawable in the ImageView and turn it into a bitmap?

我有一个动态的、可点击的 ImageView。基本上,一旦用户单击 ImageView,s/he 就可以添加要在视图内显示的图像。我想找到一种方法来获取 ImageView 中的当前图像并将其转换为位图。

到目前为止,这是我的尝试:

Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), (BitmapDrawable)imageView.getDrawable());

但它给了我一个 Wrong 2nd argument type 错误,因为它需要一个整数。

为了清楚起见,我暂时保持 post 简短。每个请求我可以 post 更多代码。

使用此方法获取位图:

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

decodeResource 为您提供来自资源的位图, 例如,如果你想从 drawable 文件夹中读取图像,你需要 decodeResource 但在这种情况下你可以直接从 imageview 获取位图