I tried compressing my image in my android app by using:
Bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
But I am getting the above error. What's the best way for me to do this without experiencing the error?
I tried compressing my image in my android app by using:
Bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
But I am getting the above error. What's the best way for me to do this without experiencing the error?
The compress()
method of Bitmap
is an "instance method" (as opposed to a "static method"). This means that it must be invoked on an actual existing Bitmap
object, rather than on the Bitmap
class itself.
In other words:
Bitmap uncompressed = /* some bitmap you've gotten from somewhere */
ByteArrayOutputStream out = new ByteArrayOutputStream();
uncompressed.compress(..., out);
Bitmap compressed = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
Here were are invoking compress()
on the uncompressed
bitmap instance.
On some level, this makes intuitive sense. If you were able to simply write:
Bitmap compressed = Bitmap.compress(...);
Then you'd have to ask yourself: what are you compressing?