How to Create a Blob from Bitmap in Android Activity?

13,485

Solution 1

You have to convert your Bitmap into a byte[]and after you can store it in your database as a Blob.

To convert a Bitmap into a byte[], you can use this :

Bitmap yourBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();

I hope it's what you want.

Solution 2

you can use this code :

public static byte[] getBytesFromBitmap(Bitmap bitmap) {
    if (bitmap!=null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
        return stream.toByteArray();
    }
    return null;
}

for converting bitmap to blob.

note:

blob is an binary large object.

Share:
13,485
Rami
Author by

Rami

Updated on June 06, 2022

Comments

  • Rami
    Rami almost 2 years

    I'm trying to create new MyImage entitly as listed in How to upload and store an image with google app engine.

    Now I'm not using any Form. I have Android app that gets Uri from Gallery :

    m_galleryIntent = new Intent();
    m_galleryIntent.setType("image/*");
    m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    
    m_profileButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) 
        {
            startActivityForResult(Intent.createChooser(m_galleryIntent, "Select Picture"),1);      
        }
    });
    

    And I'm using the Uri to create a Bitmap.
    How can I create a Blob In my client from the Bitmap?
    And what jars i'll have to add to my android project?

    Is this a proper way to use Blob?
    My main goal is to save an image uplodaed from an android in the GAE datastore, Am Using this tools properly or ther is better way? Thatks.