0

I'm using this code to get the bitmap placed inside the ImageView]

    private Bitmap getBitmap(ImageView imageView) {
          imageView.setDrawingCacheEnabled(true);

          imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
          imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); 

          imageView.buildDrawingCache(true);
          Bitmap b = Bitmap.createBitmap(imageView.getDrawingCache());
              imageView.setDrawingCacheEnabled(false);
                    return b;
                }});

and this code to save it to the database in the form of a byte array

 public byte[] convertToBytes(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    Log.d("IMAGE BYTE IS ", imageBytes.toString());
    return imageBytes;
}

Decoding the image using this code to redisplay it returns null

 public Bitmap convertToImage(byte[] bytes) {

    ByteArrayInputStream imageStream = new ByteArrayInputStream(bytes);
    return BitmapFactory.decodeStream(imageStream);
}

I have logged the byte array before inserting and after retrieving and they both return the same values. Where did it go wrong?

4

1 回答 1

0

原来我只是将流的内存引用保存到 SQLite。使用 ContentValue 将字节数组插入到 sqlite 就可以了。

于 2011-05-10T09:22:14.490 回答