我正在尝试将以 Base64 格式存储在数据库中的图像转换为要在 Imageview 中使用的位图。
因此,我以这种方式将其存储在 SQLite 中:
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap fotoGrande=(Bitmap) extras.get("data");
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
//I am adding some data to EXIF here, add to an static ArrayList<Bitmap> in other class and I store it this way:
int bytes=listaFotos.get(i).getFoto().getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
values.put("foto", Base64.encodeToString(buffer.array(), Base64.DEFAULT));
稍后,我需要获取该图像以使其适合 ImageView:
String foto = csr2.getString(0);//csr2 is a cursor
byte[] arrayFoto = Base64.decode(foto, Base64.DEFAULT);//This is not null
Bitmap fotoBitmap = BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);//This is null
我知道有很多关于此的问题。我进行了搜索,但没有答案可以解决我的问题。
为什么我的 BitmapFactory.decodeByteArray 返回 null?我做错了什么?有帮助吗?
谢谢。
最佳答案
原来是数据库问题。 SQLite 给你一个 1MB MAX 大小的游标。我正在从数据库中获取字节,光标为 1MB,图片发送不正确。
为了修复它,我将照片的路径而不是字节存储在数据库中。
关于android - 为什么我的 BitmapFactory.decodeByteArray 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31538548/