60

に Web からの画像がありImageViewます。これは非常に小さく (ファビコン)、SQLite データベースに保存したいと考えています。Drawablefromを取得できますmImageView.getDrawable()が、次に何をすべきかわかりません。DrawableAndroid のクラスがよくわかりません。

私は次のBitmapようなものからバイト配列を取得できることを知っています:

Bitmap defaultIcon = BitmapFactory.decodeStream(in);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);

byte[] bitmapdata = stream.toByteArray();

しかし、からバイト配列を取得するにはどうすればよいDrawableですか?

4

5 に答える 5

142
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();
于 2010-12-14T04:19:09.383 に答える
22

すべてに感謝し、これは私の問題を解決しました。

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();
于 2012-04-01T18:03:13.977 に答える
7
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tester);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
于 2012-07-16T10:05:16.363 に答える
0

Drawable が BitmapDrawable の場合、これを試すことができます。

long getSizeInBytes(Drawable drawable) {
    if (drawable == null)
        return 0;

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    return bitmap.getRowBytes() * bitmap.getHeight();
}

Bitmap.getRowBytes()は、ビットマップのピクセルの行間のバイト数を返します。

詳細については、このプロジェクトを参照してください: LazyList

于 2014-06-13T14:36:57.973 に答える
-2
File myFile = new File(selectedImagePath);

byte [] mybytearray  = new byte [filelenghth];

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

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

これで、イメージは bytearray に格納されます。

于 2010-12-14T06:09:51.480 に答える