如何字节数组转换为位图位图、数组、转换为、字节

由网友(十年)分享简介:我要存储图像 SQLite数据库。我试图用 BLOB 和字符串,在这两种情况下,它存储来存储它图像,并且可以使用检索它,但是当我将其转换为位图BitmapFactory.de codeByteArray(...)它返回null。 我已经使用这个code,但它返回null 位图位= BitmapFactory.de...

我要存储图像 SQLite数据库。 我试图用 BLOB 字符串,在这两种情况下,它存储来存储它 图像,并且可以使用检索它,但是当我将其转换为位图 BitmapFactory.de codeByteArray(...)它返回null。

我已经使用这个code,但它返回null

 位图位= BitmapFactory.de codeByteArray(BLOB,0,blob.length);
 

解决方案

只是试试这个:

 位图位= BitmapFactory.de codeFILE(/路径/图像/ image.jpg文件);
ByteArrayOutputStream BLOB =新ByteArrayOutputStream();
bitmap.com preSS(比较pressFormat.PNG,0 / *忽略PNG * /,BLOB);
字节[]的BitmapData = blob.toByteArray();
 

//如果的BitmapData是字节数组,然后得到位图是这样的

 位图位= BitmapFactory.de codeByteArray(的BitmapData,0,的BitmapData .length);
 
如何把一个byte数组转化为字符串

返回 去codeD的位图,或NULL,如果图像不能去code。

I want to store image in SQLite DataBase. I tried to store it using BLOB and String, in both cases it store the image and can retrieve it but when i convert it to Bitmap using BitmapFactory.decodeByteArray(...) it return null.

I have used this code, but it returns null

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

解决方案

Just Try this :

Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
byte[] bitmapdata = blob.toByteArray();

//if bitmapdata is the byte array then getting bitmap goes like this

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

Returns The decoded bitmap, or null if the image could not be decode.

阅读全文

相关推荐

最新文章