gpt4 book ai didi

android - 相机始终将 resultCode 返回为 0

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:04:01 26 4
gpt4 key购买 nike

我正在尝试在我的 android 应用程序中使用摄像头进行开发。

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

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

最佳答案

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

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

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

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

关于android - 相机始终将 resultCode 返回为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com