12

I am trying to develop using camera in my android application.

The problem is that the camera always returns a result code of 0, irrespective of if I press done or cancel. The code snippet I use is as follows:

protected void startCameraActivity()
{

    Log.i("MakeMachine", "startCameraActivity()" );

    File file = new File( _path );
    Uri outputFileUri = Uri.fromFile( file );

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
    startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{   

    Log.i( "MakeMachine", "resultCode: " + resultCode );

    switch( resultCode )
    {
        case 0:
            Log.i( "MakeMachine", "User cancelled" );
            break;

        case -1:
            Log.i( "MakeMachine", "User done" );
            onPhotoTaken();
            break;
    }
}

The logcat shows:

05-31 14:58:15.367: E/asset(29114): MAS: getAppPckgAndVerCode package: makemachine.android.examples === version 1
05-31 14:58:15.398: D/dalvikvm(29114): Trying to load lib lib_glossary.so 0x0
05-31 14:58:15.414: D/dalvikvm(29114): Added shared lib lib_glossary.so 0x0
05-31 14:58:26.125: I/MakeMachine(29114): ButtonClickHandler.onClick()
05-31 14:58:26.125: I/MakeMachine(29114): startCameraActivity()
05-31 14:58:26.507: W/IInputConnectionWrapper(29114): showStatusIcon on inactive InputConnection
05-31 14:58:36.375: I/MakeMachine(29114): User cancelled
05-31 14:58:36.375: I/MakeMachine(29114): resultCode: 0
05-31 14:58:50.945: I/MakeMachine(29114): ButtonClickHandler.onClick()
05-31 14:58:50.945: I/MakeMachine(29114): startCameraActivity()
05-31 14:58:51.429: W/IInputConnectionWrapper(29114): showStatusIcon on inactive InputConnection
05-31 14:59:01.554: I/MakeMachine(29114): User cancelled
05-31 14:59:01.554: I/MakeMachine(29114): resultCode: 0
Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
Akhila Nair
  • 291
  • 2
  • 6
  • 10

6 Answers6

11

The issue (in android >= 5.0) might be with singleInstance mode.

if you have your activity launchMode set to singleInstance, then in android < 5.0 you will receive cancelled result immediately. In android >=5.0 you will have resultCode == Activity.RESULT_CANCELED.

Try using launchMode = singleTask. It is much like singleInstance, but allows other activities to be launched on the task.

More info here: https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
babay
  • 4,689
  • 1
  • 26
  • 40
  • Issue is fixed after changing from singleInstance to singleTask but still have confusion, why this works? Any other info please – sagar.android Jun 06 '22 at 12:41
9

As per the comments section, the reason that the resultCode was returning 0 (meaning the result was cancelled) is because when taking a picture to save to the SD card, you need to add the WRITE_EXTERNAL_STORAGE permission to your manifest.

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
2

Also, sometimes the issue causes due to not personally adding a required subfolder. The application crashes silently resulting result code as 0 every time.

Jitu
  • 349
  • 1
  • 3
  • 16
  • This is exactly what happened to me. Definitely check the entire path exists before passing the photoURI. – Elroid Oct 13 '20 at 16:24
2

I was experiencing this same issue. Camera works on 5.0+ with lauchmode=singleInstance returning a correct "RESULT_OK" but on Android 4.0 I was getting back a 0 until:

Android 4.0 switched to "launchMode=singleTask" in the AndroidManifest.xml for the Activity calling the camera resulting in "RESULT_OK" == 1

This is useful for apps with backwards compatibility.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
1

I resolved my problem by doing the following changes...

private void takePictureFromCamera() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        photoFile = createImageFile();
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(activity,
                    "com.example.provider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            //COMPATIBILITY
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } else {
                List<ResolveInfo> resInfoList = activity.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    activity.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
            //COMPATIBILITY
            startActivityForResult(takePictureIntent, CAMERA);
        }


    }
}

I found the solution in:

link

ja12
  • 351
  • 2
  • 16
0

For android 9+ you have to add android:requestLegacyExternalStorage="true" this line to your manifest file in application tag,if not added.

Himani
  • 147
  • 9