Akhila Nair
Akhila Nair

Reputation: 291

Camera always returns resultCode as 0

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

Upvotes: 12

Views: 10246

Answers (6)

Himani
Himani

Reputation: 165

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

Upvotes: 0

ja12
ja12

Reputation: 360

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

Upvotes: 1

ATrainBrain
ATrainBrain

Reputation: 41

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.

Upvotes: 2

babay
babay

Reputation: 4844

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

Upvotes: 12

Jitu
Jitu

Reputation: 359

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

Upvotes: 2

Andrew Schuster
Andrew Schuster

Reputation: 3229

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.

Upvotes: 9

Related Questions