-1

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 ??

2Dee
  • 8,609
  • 7
  • 42
  • 53
Gourav Manuja
  • 89
  • 1
  • 10

2 Answers2

1

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);
}  
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • Sir i updated my code...after do this my bitmap problem is solved but my file is not updated throw multipart on server...i updated my code...see please – Gourav Manuja Sep 25 '18 at 09:26
  • I reverted your change, you are not supposed to completely change your question to another one once you receive an answer. Please do the following: 1. [accept the answer](https://stackoverflow.com/help/someone-answers) if it solved your initial issue. 2. Go over the [help section](https://stackoverflow.com/help) and review its content, especially the questions section. It will greatly help you in the future. 3. Since you read the help and know how to create a great question, create a new question to solve your multipart issue. Of course, please look for duplicates before posting. Best of luck! – 2Dee Sep 25 '18 at 09:37
  • Well done & good work, @GouravManuja ! Keep coding, keep smiling ;) – 2Dee Sep 26 '18 at 08:10
0

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
}
Rahul
  • 481
  • 4
  • 9