【问题标题】:java.lang.NullPointerException occur when using Compressor library [duplicate]使用 Compressor 库时发生 java.lang.NullPointerException [重复]
【发布时间】:2019-07-14 13:59:09
【问题描述】:
我在使用压缩器库压缩图像时遇到问题(https://github.com/zetbaitsu/Compressor)
当我尝试使用 Android-Image-Cropper(https://github.com/ArthurHub/Android-Image-Cropper) 进行压缩时,一切正常,
但是当我使用
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
错误
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
发生
【问题讨论】:
标签:
android
image-processing
nullpointerexception
android-image
image-compression
【解决方案1】:
您没有在意图中传递图像 URI,因此它给出了NullPointerException
试试这个:
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
这段代码摘自here。