Ich habe ein Bild aus dem Web in einer ImageView
. Es ist sehr klein (ein Favicon) und ich möchte es in meiner SQLite-Datenbank speichern. Ich kann eine Drawable
von mImageView.getDrawable()
erhalten, weiß dann aber nicht, was ich als Nächstes tun soll. Ich verstehe die Drawable
-Klasse in Android nicht vollständig.
Ich weiß, dass ich ein Byte-Array von einer Bitmap
erhalten kann:
Bitmap defaultIcon = BitmapFactory.decodeStream(in);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Aber wie bekomme ich ein Byte-Array von einer Drawable
?
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();
Vielen Dank und das hat mein Problem gelöst.
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();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tester);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
Wenn Drawable eine BitmapDrawable ist, können Sie diese ausprobieren.
long getSizeInBytes(Drawable drawable) {
if (drawable == null)
return 0;
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
return bitmap.getRowBytes() * bitmap.getHeight();
}
Bitmap.getRowBytes () gibt die Anzahl der Bytes zwischen den Zeilen in den Pixeln der Bitmap zurück.
Weitere Informationen finden Sie in diesem Projekt: LazyList