Android 如何将字节数组中的图像文件数据转换为位图?

Android 如何将字节数组中的图像文件数据转换为位图?,android,arrays,sqlite,bitmap,Android,Arrays,Sqlite,Bitmap,我想将图像存储在SQLite数据库中。 我尝试使用BLOB和String存储它,在这两种情况下,它都存储 图像并可以检索它,但当我使用 BitmapFactory.decodeByteArray(…)它返回null 我使用过这段代码,但它返回null Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length); 试试这个: Bitmap bitmap = BitmapFactory.decodeFile("/path

我想将图像存储在SQLite数据库中。 我尝试使用
BLOB
String
存储它,在这两种情况下,它都存储 图像并可以检索它,但当我使用
BitmapFactory.decodeByteArray(…)
它返回null

我使用过这段代码,但它返回null

Bitmap  bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
试试这个:

Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();
如果
bitmapdata
是字节数组,则获取
Bitmap
的操作如下:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

返回解码后的
位图
,如果图像无法解码,则返回
null

Uttam的答案对我不起作用。当我这样做时,我只是得到空值:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
在我的例子中,bitmapdata只有像素的缓冲区,因此函数decodeByteArray无法猜测宽度、高度和颜色位使用哪一个。所以我尝试了这个,它成功了:

//Create bitmap with width, height, and 4 bytes color (RGBA)    
Bitmap bmp = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(bitmapdata);
bmp.copyPixelsFromBuffer(buffer);

检查不同的颜色选项

请阅读本页“相关”部分中的前5-10个链接。在写入数据库之前是否对位图进行了编码?如果您试图解码的是其他格式的图像,则无法解码。如果我需要按顺序多次执行此操作,该怎么办?每次创建新的位图对象不都会消耗资源吗?我能把我的数组解码成现有的位图吗?当你们只有一个像素缓冲区的时候,我会给出一个不同的答案。由于缓冲区中缺少with、height和color,所以我总是得到null。希望有帮助!什么是mBitmaps?@Julian How to byte[]在从Sqlite重新修改图像时无法转换为java.lang.String