implicit intent for camera capture in android

6.2k views Asked by At

I am facing a very weird issue. I have an implicit intent for opening the default camera.

There is an image view with a camera icon, which when clicked opens the default camera.

Sometimes on clicking the imageview, opens the camera but sometimes it does not. When I click the back button, it opens the default camera. I am perplexed with this. Is it an issue in my code or device bug.

The image view is in the grid view. As I am creating a grid view of images captured. The first image will be the camera icon and then grid will have the captured images.

I am posting my code:

Manifest permission:

<uses-feature android:name="android.hardware.camera" />

java code:

public void openCamera(String _path, String file_name) {
    File file = new File(_path);
    if (!file.exists()) {
        file.mkdirs();
    }

    mImageFile = new File(file + file_name);
    // create new Intent
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
    mActivity.startActivityForResult(intent, 1);
}

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

    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){

            //sets the adapter

        }
    }
}
1

There are 1 answers

2
Biraj Zalavadia On

TRY this And Enjoy

File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
String imgPath = file.getAbsolutePath();

final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == CAPTURE_IMAGE) {
                imgUser.setImageBitmap(BitmapFactory.decodeFile(imgPath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }