Why is my BitmapFactory.decodeByteArray returning null?

半世苍凉 提交于 2020-01-06 03:09:04

问题


I am trying to convert an image stored in Database in Base64 format, to a Bitmap to be used in an Imageview.

So, I store it in SQLite this way:

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));

Later, i need to get that image to fit it in an 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

I know there are tons of questions about this. I searched, but no answer fix my problem.

Why is my BitmapFactory.decodeByteArray returning null? What I am doing wrong? Any help?

Thank you.


回答1:


Turned out to be a database issue. SQLite gives you a cursor of 1MB MAX size. I was getting the bytes from database, with a 1MB cursor, the pic was not sent properly.

To fix it, I stored the path to the photo in database instead of the bytes.




回答2:


firstly image is convert in Bitmap to String like this

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
        camera.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte imageInByte[] = stream.toByteArray();
        String encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);

where camera is a Bitmap. and store the String encodedImage in database

And getImage string like this

 byte[] b = Base64.decode(encodedImage , Base64.DEFAULT);
        Bitmap  bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);


来源:https://stackoverflow.com/questions/31538548/why-is-my-bitmapfactory-decodebytearray-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!