将字节转换为位图

时间:2013-03-19 16:20:41

标签: java android byte

我必须将一个字节转换为Bitmap,然后将其设置为imageview

我确实有将ImageView中的Bitmap转换为Byte并稍后插入的方法,

public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) {

             Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap();
            ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageByteStream);
            byte[] imageByteData = imageByteStream.toByteArray();
            return imageByteData;

}

当我想从数据库中检索图像并在ImageView中显示它时

 //--
             byte[] image_b   = c.getBlob(4);
             Bitmap b = BitmapFactory.decodeByteArray(image_b, 0, image_b.length);
             img.setImageBitmap(b);

但它什么都不返回

有什么不对,请帮忙

非常感谢

3 个答案:

答案 0 :(得分:1)

我猜是因为光标没有返回任何内容。也许Blob太大而无法一次查询,因为每个查询的android游标限制为1 mb请再次检查你的stacktrace / logcat,如果blob大小是问题,你可以尝试将字节拆分为更小的大小并将其存储在你的数据库

答案 1 :(得分:0)

您可以使用以下内容并告知我们是否有效:

String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = resolver.query(imageUri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        return BitmapFactory.decodeFile(picturePath);

答案 2 :(得分:0)

我不明白问题在哪里我喜欢这个:String t = image_b.toString(); System.out.println(“它给我什么”+ t);我在logcat中找到这一行:03-19 17:01:28.167:I / System.out(1019):它给了我什么[B @ 41f1f5c0

是指它返回的东西但是没有显示或字节[B@41f1f5c0不正确

相关问题