Camera始终将resultCode返回为0

时间:2012-05-31 10:11:33

标签: android

我正在尝试使用我的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

5 个答案:

答案 0 :(得分:5)

根据评论部分,resultCode返回0(意味着结果被取消)的原因是因为在拍照保存到SD卡时,您需要添加{{1}对你的清单的许可。

答案 1 :(得分:3)

问题(在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

答案 2 :(得分:2)

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

  

Android 4.0切换到" launchMode = singleTask"在里面   AndroidManifest.xml为Activity调用相机产生的结果   " RESULT_OK" == 1

这对于具有向后兼容性的应用非常有用。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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


    }
}

我找到了解决方案:

link