I need to send a PNG to a server. One very simple solution would be to create a Bitmap and convert it to a byte[] with the following code:
final Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.some_image);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, os);
final byte[] data = os.toByteArray();
Since I want to save time and memory I would like to achieve this without the need of creating a Bitmap.
One idea would be to access the Drawable as a File but I don’t know how to get the correct path.
Any ideas?
harism gave me the final hint: use one of the
Resoureces.openRawResourcemethods.Here is my final solution:
In my case I had an PNG of 250×200 px and a file size of 42046 Byte. The
Bitmapapproach needs around 500ms and the raw approach 3ms.Hope someone can use this solution.