12

我正在尝试在我的 android 应用程序中使用相机进行开发。

问题是相机总是返回结果代码 0,不管我是按完成还是取消。我使用的代码片段如下:

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;
    }
}

logcat 显示:

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
4

5 回答 5

10

问题(在 android >= 5.0 中)可能与singleInstance模式有关。

如果您将活动launchMode设置为singleInstance,那么在 android < 5.0 中,您将立即收到取消的结果。在 android >=5.0 中,您将拥有resultCode == Activity.RESULT_CANCELED.

尝试使用launchMode = singleTask. 它很像singleInstance,但允许在任务上启动其他活动。

更多信息在这里: https ://developer.android.com/guide/topics/manifest/activity-element.html#lmode

于 2016-02-18T00:17:40.463 回答
9

根据评论部分,resultCode返回 0(意味着结果被取消)的原因是因为在拍照保存到 SD 卡时,您需要将WRITE_EXTERNAL_STORAGE权限添加到清单中。

于 2014-06-02T13:23:32.370 回答
2

我遇到了同样的问题。相机在 5.0+ 上工作,lauchmode=singleInstance 返回正确的“RESULT_OK”,但在 Android 4.0 上我得到了 0,直到:

Android 4.0 切换到 AndroidManifest.xml 中的“launchMode=singleTask”,用于调用相机的 Activity 导致“RESULT_OK”== 1

这对于具有向后兼容性的应用程序很有用。

于 2016-03-21T18:23:20.107 回答
2

此外,有时由于未亲自添加所需的子文件夹而导致问题。应用程序每次都会默默地崩溃,结果代码为 0。

于 2015-11-09T13:10:40.193 回答
1

我通过进行以下更改解决了我的问题...

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);
        }


    }
}

我在以下位置找到了解决方案:

关联

于 2018-03-12T14:24:13.880 回答