Valid byte []

I have an image from the Internet in ImageView . It is very small (icon) and I would like to save it in my SQLite database. I can get Drawable from mImageView.getDrawable() , but then I don't know what to do next. I do not quite understand the Drawable class in Android.

I know that I can get an array of bytes from Bitmap , for example:

 Bitmap defaultIcon = BitmapFactory.decodeStream(in); ByteArrayOutputStream stream = new ByteArrayOutputStream(); defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] bitmapdata = stream.toByteArray(); 

But how to get an array of bytes from Drawable ?

+45
android database drawable android-bitmap
Dec 14 '10 at 4:14
source share
5 answers
 Drawable d; // the drawable (Captain Obvious, to the rescue!!!) Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] bitmapdata = stream.toByteArray(); 
+108
Dec 14 '10 at 4:19
source share

Thanks to everyone and this solved my problem.

 Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.my_pic); Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] bitMapData = stream.toByteArray(); 
+18
Apr 01 '12 at 18:03
source share
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tester); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] bitMapData = stream.toByteArray(); 
+5
Jul 16 '12 at 10:05
source share

If Drawable is BitmapDrawable, you can try this.

 long getSizeInBytes(Drawable drawable) { if (drawable == null) return 0; Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); return bitmap.getRowBytes() * bitmap.getHeight(); } 

Bitmap.getRowBytes () returns the number of bytes between lines in raster pixels.

For more details see this project: LazyList

0
Jun 13 '14 at 2:36
source share
 File myFile = new File(selectedImagePath); byte [] mybytearray = new byte [filelenghth]; BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile)); bis1.read(mybytearray,0,mybytearray.length); 

now the image is saved in bytes.

-2
Dec 14 2018-10-12T00:
source share



All Articles