0

I try to capture image from android and upload to server , I did all the code runtime permissions, uploading to server and all. but my bitmap from camera intent is null.I tries getExtras and all way. For me the code seems to be correct but bitmap is always null. What could be the reason

Tried of using

 data.getExtras().get("data"); 

as here onActivityResult returned from a camera, Intent null

but didn't help

My code to perform camera intent on a button click

  CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            intent = new 
   Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(intent, 7);

        }
    });


 // Star activity for result method to Set captured image on image view 
 after click.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 7 && resultCode == RESULT_OK && data != null && 
   data.getData() != null) {

        Uri uri = data.getData();

        // Adding captured image in bitmap.
        // if(data.getData()==null){
        bitmap = (Bitmap)data.getExtras().get("data");
        //}//else{
        // bitmap = 
    MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
        //}
        //  bitmap = 
    MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

        // adding captured image in imageview.
        ImageViewHolder.setImageBitmap(bitmap);

    }

}

i want to get bitmap from camera intent and show in imageview.

jasvik
  • 75
  • 7
  • please check this https://stackoverflow.com/a/2737770/10815148 – Dhaiyur Apr 30 '19 at 10:56
  • 1
    `if (requestCode == 7 && resultCode == RESULT_OK && data != null && data.getData() != null)` -- get rid of the `data.getData() != null` part. That is *supposed* to be `null`. – CommonsWare Apr 30 '19 at 11:05
  • Same as @CommonsWare remove all code with getData(), also using this way you are getting the Thumbnail not the full image, but i guess this is what you want – User One Apr 30 '19 at 11:16
  • @CommonsWare it works fine now thank you. Post it as a answer ll accept it – jasvik Apr 30 '19 at 11:17
  • Same Answer Mentioned here [Link](https://stackoverflow.com/questions/37628580/camera-intent-returns-null-onactivityresult) – Ganesh Pokale Apr 30 '19 at 11:21

2 Answers2

1

In onActivityResult(), you have:

if (requestCode == 7 && resultCode == RESULT_OK && data != null && 
    data.getData() != null) {

The first three parts of that are fine. However, data.getData() != null has two problems:

  1. It is not relevant, in that the if block does not attempt to use data.getData() (though you have commented-out code that does)

  2. data.getData() is supposed to be null for ACTION_IMAGE_CAPTURE, so for correctly-implemented camera apps, your if condition will always be false

If you remove data.getData() != null, you should go into the if block and be able to try to get your Bitmap.

As others have noted, this code will give you a thumbnail-sized Bitmap. If you are looking for the camera app to give you a full-resolution image, you will need to use EXTRA_OUTPUT. This sample app demonstrates how this is done.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0
private void choosePhotoFromCamera() {
        log.info("choosePhotoFromCamera() is called");

        intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
    }


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == this.RESULT_CANCELED) {
        return;
    }

    // choose profile image from CAMERA
    if (requestCode == 1) {
        try {

           Bitmap bitmap = (Bitmap) data.getExtras().get("data");

            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
                strImagePath = getImageUri(bitmap).toString();
            }

        } catch (Exception e) {
            log.error("Exception after choosing image from camera : " + e.getMessage());
        }
    }
mohit
  • 88
  • 9
  • edited my answer... and now it will works perfectly. – mohit Apr 30 '19 at 11:11
  • I asked for some explanations not more code . You should indicate what was wrong with OP's code and what corrections you have made to make it work .. Thats what i call an Complete answer ... – ADM Apr 30 '19 at 11:20
  • It look like there is no checked exception in your code, why try/catch – User One Apr 30 '19 at 11:24
  • if you capturing the image.. then, Uri uri = data.getData(); will always return null...this will works when you selecting the image from gallery.. – mohit Apr 30 '19 at 11:25
  • bitmap = (Bitmap)data.getExtras().get("data"); this line is enough to hold bitmap... – mohit Apr 30 '19 at 11:25