【发布时间】:2016-10-17 12:54:44
【问题描述】:
我将使用Bitmap.compress() 方法压缩图像。
但是当我使用bitmap = BitmapFactory.decodeFile() 得到Bitmap 时,我得到了一个null 对象,并且该方法没有出现任何异常。
这是我的代码
public static File compressImage(String imagePath) throws IOException {
// Get bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
// Get bitmap output stream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
......
当代码在最后一行运行时,我得到一个NullPointerException:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
然后我在调试模式下运行我的代码,结果我从BitmapFactory.decodeFile 方法中得到了一个null 对象。
参数imagePath是
/storage/emulated/0/DCIM/Camera/IMG_20160610_195633.jpg
看起来还可以。
这段代码在另一个活动中运行良好,但是当我将它复制到我尝试压缩和上传图像的异步线程时,它崩溃了。有没有可能这是因为异步线程的事情?还是我没有注意到的其他东西?
【问题讨论】:
标签: android bitmap bitmapfactory