非静态方法'compress(android.graphics.Bitmap.CompressFormat,int,java.io.OutputStream)'不能从静态上下文引用

问题描述 投票:-2回答:1

我尝试使用以下方法在我的Android应用中压缩我的图像:

Bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);

但我得到了上述错误。在没有遇到错误的情况下,我最好的方法是什么?

android android-bitmap
1个回答
1
投票

compress() methodBitmap是一种“实例方法”(与“静态方法”相对)。这意味着它必须在实际存在的Bitmap对象上调用,而不是在Bitmap类本身上调用。

换一种说法:

Bitmap uncompressed = /* some bitmap you've gotten from somewhere */
ByteArrayOutputStream out = new ByteArrayOutputStream();
uncompressed.compress(..., out);
Bitmap compressed = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

这里是在compress()位图实例上调用uncompressed

在某种程度上,这具有直观意义。如果你能够简单地写:

Bitmap compressed = Bitmap.compress(...);

然后你必须问自己:你在压缩什么?

© www.soinside.com 2019 - 2024. All rights reserved.