BitmapFactory.decode method returning null
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?
Turns out I was only saving the memory reference of the stream to the SQLite. Using ContentValue to insert the byte array to sqlite did the trick.
精彩评论