How to get a byte array from a drawable resource ?

守給你的承諾、 提交于 2019-12-29 08:49:15

问题


I would like to get a byte array from an jpeg image located in my res/drawable file ?

Does anyone know how to do that please ?


回答1:


Get a bitmap decodeResource(android.content.res.Resources, int) Then either compress it to ByteArrayOutputStream() or copyPixelsToBuffer and get your array from the buffer. http://developer.android.com/reference/android/graphics/Bitmap.html




回答2:


    Drawable drawable;

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



回答3:


ByteArrayOutputStream stream = new ByteArrayOutputStream();
mPhoto.compress(Bitmap.CompressFormat.JPEG /* FileType */,
                        100 /* Ratio */, stream);

HTH !



来源:https://stackoverflow.com/questions/2829469/how-to-get-a-byte-array-from-a-drawable-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!