0

I'm having trouble compressing image using compressor library(https://github.com/zetbaitsu/Compressor)
when I try compressing using Android-Image-Cropper(https://github.com/ArthurHub/Android-Image-Cropper) Everything works well, but when I use

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

The error

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

occure

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Supagon Srisawas
  • 93
  • 1
  • 1
  • 9

1 Answers1

1

You are not passing the image URI in the intent so it is giving NullPointerException

Try this:

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"));

This code excerpt is taken from here.

nimi0112
  • 1,753
  • 1
  • 13
  • 29