如何字节数组转换为位图 [英] How to convert byte array to Bitmap

查看:271
本文介绍了如何字节数组转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储图像 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);
 

返回 去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.

这篇关于如何字节数组转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆