Frage

Ich habe ein Bild von der Bahn in einer ImageView. Es ist sehr klein (ein Favicon) und ich mag es Datenbank in meinem SQLite speichern. Ich kann ein Drawable von mImageView.getDrawable() bekommen, aber dann weiß ich nicht, was als nächstes zu tun ist. Ich verstehe nicht ganz die Klasse Drawable in Android.

Ich weiß, ich kann eine Byte-Array erhalten von einem Bitmap wie:

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 einem Drawable?

War es hilfreich?

Lösung

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();

Andere Tipps

Durch all diese und 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 ein BitmapDrawable ist, können Sie versuchen diese.

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 Reihen in der Bitmap der Pixel.

Für mehr dieses Projekt verweisen: LazyList

File myFile = new File(selectedImagePath);

byte [] mybytearray  = new byte [filelenghth];

BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile));

bis1.read(mybytearray,0,mybytearray.length);

nun das Bild in der bytearray gespeichert wird ..

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top