Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将字节数组中的图像文件数据转换为位图?_Android_Arrays_Sqlite_Bitmap - Fatal编程技术网

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 = 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