如何使用PNG格式压缩位图?

How to compress Bitmap using PNG format?

我有一个自定义视图 (WorldMapView) 和一个可绘制地图 (3209x1287,PNG,64 位颜色,1.33M),我得到

java.lang.RuntimeException: Canvas: trying to draw too large(148679388bytes) bitmap.

我试过这样压缩的

  WorldMapView worldMapView = (WorldMapView) itemView.findViewById(R.id.map_of_world);
  Bitmap bitmap = ((BitmapDrawable) ContextCompat.getDrawable(context, R.drawable.map)).getBitmap();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
  worldMapView.setImageBitmap(decoded);

但是如果您使用的是 PNG 格式,那么它不会压缩您的图像,因为 PNG 是一种无损格式,所以有人可以提出解决方案吗?

提前致谢。

I tried to compress like this

你不能 "compress" 一个 Bitmap 到另一个 Bitmap。图片以某些文件格式(例如 PNG、JPEG、WebP)压缩在磁盘上Bitmap 总是 未压缩。

if you are using PNG format then it will not compress your image because PNG is a lossless format

这与您的问题无关。 PNG 是一种磁盘格式。 Bitmap 不是。从 BitmapBitmap 不会 "compress" 位图,无论您使用什么图像格式。

so can anyone please suggest a solution?

将图像的分辨率降低 90% 左右。

或者,使用处理大图像的视图,例如 this one