-2

I am storing image as byte array in sqlite database from one project. Then i use the pre-populated table in another project

But the problem is that Bitmap Factory is resulting as null.

Code of storing images:

Bitmap myLogo = BitmapFactory.decodeResource(getResources(), arr[i]);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
myLogo.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
b = stream1.toByteArray();

Code of retrieving images:

BitmapFactory.Options options = new BitmapFactory.Options();
decodedByte = BitmapFactory.decodeByteArray(Image, 0,Image.length, options);
System.out.println("Image = " + Image);
System.out.println("decodedByte = " + decodedByte);

Here Image is returning the byteArray(Image) with length 12. But the bitmap(decodedByte) is returning null value.

I have tried a lot of ways but cannot find a solution. please help!

Carbon
  • 133
  • 1
  • 4
  • 21
  • The code looks okay, are you sure you are storing / retrieving the correct data in the db? Length 12 is far from enough to store an image and `BitmapFactory` will return `null` if it could not decode the data. I guess that's what's happening. – zapl Jul 22 '13 at 12:24
  • @zapl : yes you were right! before i store the length is 72219. and after retrieving its is only 12. what can i do about it? – Carbon Jul 23 '13 at 04:40

2 Answers2

0

this is my function from bitmap to string

    public String BitMapToString(Bitmap bitmap) {
    try {
        Bitmap encogida = Bitmap.createScaledBitmap(bitmap, 226, 180, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        encogida.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        return org.kobjects.base64.Base64.encode(b);
    } catch (Exception ex) {
        Conexiones.escribirLog(Log.getStackTraceString(ex),
                getString(R.string.versionReal));
        Log.e("ERROR BitMapToString ", ""); // ex.getMessage());
        return "";
    }
}

I hope it helps

Alex Muni
  • 473
  • 5
  • 16
0

Have you tried decodeByteArray without the options parameter ? I think it's useless in this case.

Anyway, it's better to store image URI as String instead of a byte array in sqlite database.

To get URI :

        String path = Images.Media.insertImage(getContentResolver(), mylogo,
                "title", null);

        Uri uriMyLogo = Uri.parse(path);

        String uriString = uriMyLogo.toString() ;

And to get Image from URI

                Uri uriLogo = Uri.parse(uriString);

                Bitmap logo = null;
                try {
                    logo = Media.getBitmap(this.getContentResolver(), uriLogo);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

I hope it helps if you can't manage it as you want.

David N
  • 509
  • 4
  • 12