相机始终将resultCode返回为0 [英] Camera always returns resultCode as 0

查看:76
本文介绍了相机始终将resultCode返回为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

问题是无论我按完成还是取消,相机始终返回结果代码0.我使用的代码段如下:

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

日志显示:

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

推荐答案

问题(在android> = 5.0中)可能是 singleInstance 模式.

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

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

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.

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

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

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

这篇关于相机始终将resultCode返回为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆