0

I make an application that can update images to the server. There are 2 options : Take picture from camera and select from library. the code works for Select from library choice and when I click on Take picture the app crash with these report. How to handle this ? My code for capture image intent :

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

            captureImage();
        }
    });


private void captureImage() {
    Intent intentCap = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intentCap.setType("image/*");
    startActivityForResult(intentCap, 0);
}

error

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Sơn Tùng
  • 11
  • 6

4 Answers4

0

Seems like Android cant found suitable Intent for this.

Try this Intent:

try{
   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
   startActivityForResult(cameraIntent, CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
// show message to user 
}
Anton A.
  • 1,718
  • 15
  • 37
0

You should always check for the resolveActivity when calling intent like this of third party.

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

Refer this

Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
0

First and foremost check that you have added the required permissions in your Manifest file and then test your app. Even if the app doesn't run after that then that maybe occurring because of the following reasons: 1) There may not be a camera on your device 2) There is no SD card in your phone

In this case you can refer the following links which describe solutions to the similar problem link 1 and link 2

Sudhanshu Vohra
  • 1,345
  • 2
  • 14
  • 21
0

Most probably it could be possible that you must have forgot to add Runtime Permissions to access Camera API and thus resulting in App Crash when you try to open camera. Below is the code snipped which you can use to do the same:

public void showCamera(View view) {

        // BEGIN_INCLUDE(camera_permission)
        // Check if the Camera permission is already available.
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            // Camera permission has not been granted.

            requestCameraPermission();

        } else {

            // Camera permissions is already available, show the camera preview.
            Log.i(TAG,
                    "CAMERA permission has already been granted. Displaying camera preview.");
            showCameraPreview();
        }
        // END_INCLUDE(camera_permission)

    }

private void requestCameraPermission() {

        // BEGIN_INCLUDE(camera_permission_request)
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.

        } else {

            // Camera permission has not been granted yet. Request it directly.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                    REQUEST_CAMERA);
        }
        // END_INCLUDE(camera_permission_request)
}

Here is Google Runtime Permission Model Video to have an better understanding.

Also check official documentation for further details.

Hope this helps.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20