public Drawable icon; \\\\ Some value in this field
I am creating a database in SQLite Manager. I want to store icon
into the database
You can convert your image into byte array and store that bytearray in blob filed in database. You can retrieve your image back from database as byte array.
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d = iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
System.out.println(".....d....."+d);
System.out.println("...bitDw...."+bitDw);
System.out.println("....bitmap...."+bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ContentValues cv = new ContentValues();
cv.put(CHUNK, buffer); //CHUNK blob type field of your table
long rawId = database.insert(TABLE, null, cv); //TABLE table name
public static Bitmap convertByteArrayToBitmap(
byte[] byteArrayToBeCOnvertedIntoBitMap)
{
bitMapImage = BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
return bitMapImage;
}
I made few changes in above answer to get byte array imageview
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
iv.setDrawingCacheEnabled(true);
Bitmap bitmap = iv.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
if you want to save multiple images to database then you can create own method as following : //retrieve images from drawable into int array:
int[] IMAGES = { R.drawable.yourimage1, R.drawable.yourimage2, R.drawable.yourimage3,
R.drawable.yourimage4, R.drawable.yourimage5 };
//create method to convert drawable images(which are in int) to byte[] as below:
public byte[] convertToByteArray(int image){
Resources resources = getResources();
Drawable drawable = resources.getDrawable(image);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress( Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapData = stream.toByteArray();
return bitmapData;
}
and now call this method as below: byte [] image = convertToByteArray(IMAGES[position]);// dbAdapter.insertValues(image);