Android 如何获取当前图像背景id
如何获取当前图像背景id 我的代码是Android 如何获取当前图像背景id,android,image,Android,Image,如何获取当前图像背景id 我的代码是 int d = iv.getId(); Bitmap image = BitmapFactory.decodeResource(getResources(),d); // convert bitmap to byte ByteArrayOutputStream stream = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte
int d = iv.getId();
Bitmap image = BitmapFactory.decodeResource(getResources(),d);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
//Log.e("insert",a);
db1.addFavorite(imageInByte);
e1.setText("");
但是它不起作用。假设
iv
是一个ImageView,并且这个ImageView的源图像是一个位图(这意味着在您的参考资料中,可绘制的最有可能是一个*.png)
此外,我假设您想要获取位图,即当前iv
ImageView的图像。不是你在问题中提到的背景
要获取位图并使用,请使用以下代码替换前两行:
Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
什么是
iv
?它需要是图像资源的id您的要求是什么?对于您的种类信息,您可以获取仅视图的id,而不是其背景,但您可以获取背景图像视图。getBackground()返回drawable not id我们需要更多的内容:什么是iv
(我假设是ImageView?)和什么db1
?为什么需要将图像作为字节流,如何确保可绘制的是位图?