如何从可绘制资源中获取字节数组?

Spr*_*dzy 4 resources android bytearray drawable

我想从位于res/drawable文件中的jpeg图像中获取一个字节数组?

有谁知道怎么做?

San*_*ndy 11

    Drawable drawable;

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
Run Code Online (Sandbox Code Playgroud)


Ale*_*voy 6

获取位图 decodeResource(android.content.res.Resources, int) 然后将其压缩为ByteArrayOutputStream()或copyPixelsToBuffer并从缓冲区获取数组. http://developer.android.com/reference/android/graphics/Bitmap.html