Java 如何从imagebutton获取位图
我有Java 如何从imagebutton获取位图,java,android,sqlite,Java,Android,Sqlite,我有ImageButton当点击它时,gallery将出现用于选择图像并发送bitmap返回以在此ImageButton上显示。 但我必须获取此图像按钮上显示的位图,然后将其作为字节[]保存到数据库中试试看 Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap(); 首先从ImgaeButton Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable(
ImageButton
当点击它时,gallery将出现用于选择图像并发送bitmap
返回以在此ImageButton
上显示。
但我必须获取此图像按钮上显示的位图
,然后将其作为字节[]保存到数据库中
试试看
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();
首先从
ImgaeButton
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();
然后将此位图
转换为byteArray
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] byteArray = outputStream.toByteArray();
您可以使用blob在sqlite android内部数据库中存储图像 ***下面的答案是完全从复制-信用卡去回答供应商 public void insertImg(int-id,位图img){ 从数据库中检索图像的步骤 公共位图getImage(int i){
当你从gallery加载图像时,你已经有了对它的URI引用,或者你有了位图
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
现在,如果您想从imageButton获取位图,可以使用
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();
另请参阅,要了解更多信息,当您必须拾取图像时,为什么不使用拾取的图像而不是从图像按钮获取图像?将位图设置为imagebutton时,只需将其存储到某个变量中即可。需要时,只需使用变量即可
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();