Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

android - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...