【问题标题】:Bitmap giving null when I decode?解码时位图为空?
【发布时间】:2013-10-30 10:39:33
【问题描述】:
byte[] imageInByte;
ImageView iViewAddImage;

if (imageInByte.length>0) {
   Bitmap bitmap = BitmapFactory.decodeByteArray(imageInByte, 0, imageInByte.length);
   iViewAddImage.setImageBitmap(bitmap);
 }

我得到了BitmapnullimageInByte.length=20621

做错了什么。建议我?

编辑 1:

public void insertImage(byte[] imageInByte2) 
{
    if(sqliteDb != null)
    {
        try {
             sqliteDb.insert("database",null,getDataValues(imageInByte2));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }   
}

   private ContentValues getDataValues(byte[] imageInByte2){
    ContentValues contentValues = new ContentValues();

    try {
        contentValues.put("image",imageInByte2);

    } 
    catch (Exception e) {
        e.printStackTrace();
    }   
    return contentValues;
  }

【问题讨论】:

  • 您在字节数组中添加了图像字节的位置?试试byte[] imageInByte=new byte[1024];
  • 你需要将图片转换成字节数组,然后才添加解码。
  • 我已将字节数组添加到数据库中并从那里重新获取... @GrIsHu
  • 你可以在存储到数据库之前发布代码如何生成字节数组

标签: android bitmap bytearray bitmapfactory


【解决方案1】:

这可能对你有帮助

byte imageinbyte[];
bitmap bmp;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                imageInByte = bos.toByteArray();

您可以直接设置图像视图背景

ImageView.setImageBitmap(bmp);|

【讨论】:

    猜你喜欢
    • 2015-12-19
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多