Android Bitmap null object reference

1.3k Views Asked by At

This is my exception:

   E/UploadBill: Error writing bitmap
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
    at kmsg.com.onetouch.activity.UploadDocumentActivity$UploadBill.doInBackground(UploadDocumentActivity.java:299)
    at kmsg.com.onetouch.activity.UploadDocumentActivity$UploadBill.doInBackground(UploadDocumentActivity.java:279)

This is my code:

  File imageFile;
  Bitmap photo;

This is my button click:

    public void getFile(View view) {
    if (ContextCompat.checkSelfPermission(UploadDocumentActivity.this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED){

        ActivityCompat.requestPermissions(UploadDocumentActivity.this,new String[]{Manifest.permission.CAMERA},
                MY_CAMERA_PERMISSION_CODE);
    } else {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        String pictureNm = getPictureName();
        imageFile = new File(pictureDirectory , pictureNm);
        Uri pictureUri = Uri.fromFile(imageFile);
        System.out.println("URI"+pictureUri);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);
        cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

    }
}

This is onActivityResult:

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
    mImgDocument.setImageBitmap(myBitmap);
}

Error is occur here where i am trying to compress Bitmap:

   try {
            os = new FileOutputStream(imageFile.getAbsolutePath());
            photo.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.flush();
            os.close();

            } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
            }
            partList.add(new FilePart("file", imageFile));
        } catch (FileNotFoundException e) {
        e.printStackTrace();
        }

I am trying to send image to server like this but i could not find what is going wrong with my code. I want to send this imagefile but when i see my logs,it shows something like this:

  I/System.out: partList:[file]

I want to send image only.Is it right way which i am trying ?? What is going wrong with Bitmap.compress ??

2

There are 2 best solutions below

3
2Dee On BEST ANSWER

Your photo variable is not initialized. You need to initialize that variable before using it.

Replace

Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); // You never use myBitmap after creating and initializing it...
mImgDocument.setImageBitmap(myBitmap);

with

if (requestCode==CAMERA_REQUEST && resultCode==RESULT_OK){ // <- this ensures the user didn't cancel the Camera Intent
    photo= BitmapFactory.decodeFile(imageFile.getAbsolutePath()); // <- this initializes the variable so you can use it later.
    mImgDocument.setImageBitmap(photo);
}  
1
Rahul On

In onActivityResult() store into 'photo' variable with that file instead of 'myBitmap'. Here that file may exist or not, so do this when capturing image is completed successfully.

if (requestCode==CAMERA_REQUEST && resultCode==RESULT_OK){
     //load bitmap
}