Camera always returns resultCode as 0

前端 未结 5 809
小蘑菇
小蘑菇 2021-01-04 10:40

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 c

相关标签:
5条回答
  • 2021-01-04 11: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

    0 讨论(0)
  • 2021-01-04 11:12

    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.

    0 讨论(0)
  • 2021-01-04 11:23

    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

    0 讨论(0)
  • 2021-01-04 11:27

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

    0 讨论(0)
  • 2021-01-04 11:30

    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.

    0 讨论(0)
提交回复
热议问题