Raksha Deshmukh
Raksha Deshmukh

Reputation: 41

onActivityResult returns null Intent

I am trying to capture image from camera. But it returns null Intent onActivityResult . Here is my code

CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 7);
    }
});

And onActivityResult is

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 7 && resultCode == RESULT_OK) {
        try {
            if(data.getData() == null) {
                bitmap = (Bitmap)data.getExtras().get("data");
            } else {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
            }

            ImageViewHolder.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I am new to Android so please explain answer in detail. Thanks in advance..!

Upvotes: 1

Views: 1851

Answers (4)

Asad
Asad

Reputation: 1429

Uri imageUri = data.getData() will no more work.. first get bitmap from data like this:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        val imageBitmap = data.extras.get("data") as Bitmap
        imageView.setImageBitmap(imageBitmap)
    }
}

then save bitmap image.. please find details here:

https://developer.android.com/training/camera/photobasics

Upvotes: 0

Mohammed Farhan
Mohammed Farhan

Reputation: 1138

You can also use setImageURI() in onActivityResult() instead of setImageBitmap()

First of all add the camera permissions in android manifest. If your android version is greater than lollipop then you need to add run time permissions See documentations here

<uses-permission android:name="android.permission.CAMERA"/>

and then your code will be

CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 7);
    }
});

and then use setImageUrI() in your onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

     switch (requestCode)
        {
            case 7:
                /*case 7 for image results and display image in imageview via Uri*/
                if (resultCode == RESULT_OK)
                {
                    Uri imageUri = data.getData();
                    ImageViewHolder.setImageURI(imageUri);
                }
                return;
           default:
                return;
        }
}

Upvotes: 0

Rahul
Rahul

Reputation: 10625

Make sure you have added camera permissions, write permissions in manifest and also cross verify run time permissions.

Use your Intent like below, and specify the path where captured image will be write -

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

   Uri outputUri = Uri.fromFile("path of your directory");
   intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

   startActivityForResult(intent, 7);

Upvotes: 0

Renuka Kapil
Renuka Kapil

Reputation: 31

Start Your work by passing intent through this :

startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);

so after capturing the image from camera use to get data about images through intent :

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Uri yourCapturedImage = intent.getData();
 }

Upvotes: 1

Related Questions