Null Pointer Exception when trying to compress Bitmap

做~自己de王妃 提交于 2019-12-05 13:23:34

You're getting a null pointer exception because bitmap is null. Replace the line that goes

bitmap = BitmapFactory.decodeResource(getResources(), R.id.imgPreview);

with this

bitmap = ((BitmapDrawable) imgPreview.getDrawable()).getBitmap();

if you are using PNG format then it will not compress your image because PNG is a lossless format. use JPEG for compressing yourimage and use 0 instead of 100 in quality.

then use

Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(stream.toByteArray()));

Try using Bitmap.createScaledBitmap

Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);

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