dongyumiao5210 2016-10-27 05:00
浏览 100

解码ByteArrray以将位图显示到ImageView Android中

Probably I'm repeating the same question but I haven't found solution to this problem & many of the same questions are still un-answered. So , here is my question :

I have implemented PHP backend where user can upload pictures in any image format & it will be saved as BLOB in MySQl database . To retrieve these images on Android device I have prepared JSON response in which these images are in form of base64 strings.

PHP Code :

$rightScrollImage= base64_encode($row['right_image']);

Now, on the Android side these image will be saved in SQLite database & here is

CODE TO INSERT IMAGE :

rightScrollImage.setRightimagedata(Base64.decode(row.getString("rightScrollerImage"), 0));

WHERE datatype for this field in SQLite also as BLOB.

CODE TO DISPLAY IMAGE :

byte[] imageData = cursor.getBlob(cursor.getColumnIndex(DatabaseManager.RIGHT_IMAGE));

Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
imageStream.reset();
holder.imageView.setImageBitmap(bitmap);

Problem with this code is it can decode few images & those which are not displayed got error of "D/skia: --- decoder->decode returned false" .

  • 写回答

1条回答 默认 最新

  • doujian3401 2016-10-27 06:47
    关注

    Try this:

    String rightScrollerImage = urlFromItsComing();
    byte[] image= Base64.decode(rightScrollerImage, Base64.DEFAULT);
    Bitmap = bmpImage = BitmapFactory.decodeByteArray(image, 0, image.length);
    someImageView.setImageBitmap(bmpImage);
    

    Update (if you save string first in db):

    SaveInColumnWith_String_Datatype(rightScrollerImage);
    String image_data = cursor.getString(cursor.getColumnIndex(DBData.ColumnName));
        byte[] image= Base64.decode(image_data, Base64.DEFAULT);
        Bitmap = bmpImage = BitmapFactory.decodeByteArray(image, 0, image.length);
        someImageView.setImageBitmap(bmpImage);
    

    If you are saving in BLOB Data Type Field, Before Saving, Convert String to byte[], like:

        byte[] image_byte_data = Base64.decode(imageDataString, Base64.DEFAULT);
    
    SaveInColumnWith_BLOB_Datatype(image_byte_data);
    
    byte[] image_data = cursor.getBlob(cursor.getColumnIndex(DBData.ColumnName));
        Bitmap = bmpImage = BitmapFactory.decodeByteArray(image_data, 0, image.length);
        someImageView.setImageBitmap(bmpImage);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)