Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a Blob from Bitmap in Android Activity?

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.

like image 715
Rami Avatar asked May 16 '12 12:05

Rami


2 Answers

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.

like image 161
grattmandu03 Avatar answered Oct 20 '22 06:10

grattmandu03


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.

like image 37
John smith Avatar answered Oct 20 '22 07:10

John smith