Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap to integer array?

Tags:

android

bitmap

in my application i have a database where i store image .i want to retrieve images.images are easly retrieve in BitMap but the problem is how can i convert that BitMap image into integer format.

this is code:

   DataBaseClass objOfDataBaseClass=new DataBaseClass(context);
    Cursor mCursor=objOfDataBaseClass.showData();//here is all data taken from dataBase

if(mCursor.moveToNext()) {

        byte[] mg=null;
        mg = mCursor.getBlob(mCursor.getColumnIndex("image"));
        Bitmap bitmap = BitmapFactory.decodeByteArray(mg, 0, mg.length);

         int[] store=?//how can i bitMap store in this array.
}
like image 895
xyz rety Avatar asked Jan 23 '26 18:01

xyz rety


2 Answers

You can use the Bitmap.getPixels method (documentation)

int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] store = new int[width * height];
bitmap.getPixels(store, 0, width, 0, 0, width, height);
like image 160
antonio Avatar answered Jan 26 '26 09:01

antonio


You shouldn't store the pixels of the bitmap itself, as a bitmap contains the image in uncompressed form and that would result in a huge amount of data (e.g. 1024 x 768 x 32 bit is 3.1 MB).

Better compress the bitmap to e.g. an PNG and store that in the database. You can use Bitmap.compress() for that purpose and use a ByteArrayOutputStream to write the data to. Then, store the bytes of that outputstream to your database.

like image 31
Ridcully Avatar answered Jan 26 '26 09:01

Ridcully



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!