android - 将 byteArray 转换为从 sqlite 检索的位图

标签 android image bitmap arrays

我正在尝试将图像保存在 sqlite 数据库中,稍后再检索它。我以byte[]的形式存储了它。但是没有形成位图!

存储图像:(blob类型)

Drawable myDrawable = getResources().getDrawable(arr[i]);
myLogo = ((BitmapDrawable) myDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
b = stream.toByteArray();

我的检索代码:

byte[] Image;
Image = c.getBlob(c.getColumnIndex("img_str"));
BitmapFactory.Options options = new BitmapFactory.Options();
decodedByte = BitmapFactory.decodeByteArray(Image, 0,Image.length, options);
arr_img.add(decodedByte);

System.out.println("Image = " + Image);
System.out.println("decodedByte = " + decodedByte);

日志:

07-22 07:15:55.482: D/skia(19319): --- SkImageDecoder::Factory returned null
07-22 07:15:55.482: I/System.out(19319): Image = [B@4057ea48
07-22 07:15:55.482: I/System.out(19319): decodedByte = null
07-22 07:15:55.521: D/dalvikvm(19319): GC_EXPLICIT freed 114K, 50% free 2871K/5639K,external 5406K/5783K, paused 43ms
07-22 07:15:55.541: D/skia(19319): --- SkImageDecoder::Factory returned null
07-22 07:15:55.541: I/System.out(19319): Image = [B@40577aa0
07-22 07:15:55.541: I/System.out(19319): decodedByte = null

请帮忙,因为我不知道为什么这不起作用。谢谢

最佳答案

您的图像字节非常小。你确定那是整个图像吗?根据您的输出,图像只有 22 个字节,而且很小。我之所以这么说,是因为 API 声明:

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

我认为 image 没有预期的那么大。

尝试使用此代码将您的图像再次存储在数据库中并再次尝试检索:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), R.drawable.yourBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
b = stream.toByteArray();

关于android - 将 byteArray 转换为从 sqlite 检索的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17782605/

相关文章:

android - 如何将位图从一个 Activity 传递到另一个 Activity

android - 什么是位图阈值?

java - Android XML 号码内的建议

android - 重复无限数量的 AnimationDrawable 并在单击按钮时停止

ios - 保存高分辨率资源,以便应用程序知道它可以在 Retina 设备上使用它们

jquery - 从内容脚本引用时,扩展程序中的图像显示为损坏的链接

java - 如何将一个 PNG 图像用于多个 Sprite ?

c - 陷入位图的实现中

android - 如何适应布局背景图像

Android SQLiteDatabase 插入错误