我正在构建一个应用程序,我需要从库中选择图片或通过相机拍摄新图片。我建立了三个活动:一个活动显示选定/拍摄的图片,一个活动选择从内存中选择图片作为图库,一个活动用相机拍摄新照片。我所做的是:当用户点击一个按钮获取更多图片时,我打开一个这样的意图选择器:
Intent galleryIntent = new Intent(Constants.INTENT_GALLERY);
Intent cameraIntent = new Intent(Constants.INTENT_PHOTO_CAPTURE);
Intent chooserIntent
= Intent.createChooser(galleryIntent, getString(R.string.title_photo_chooser));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { cameraIntent });
startActivityForResult(chooserIntent, Constants.SUCCESS);
然后画廊和相机活动完成如下:
Intent intent = new Intent();
intent.putStringArrayListExtra(Constants.INTENT_KEY_PHOTO, selected);
setResult(RESULT_OK, intent);
finish();
最后,第一个活动,观众,得到如下结果:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK){
if( requestCode == Constants.SUCCESS ) {
[...]
}
}
}
我的手机使用API 22已经证明这是成功的。不幸的是,在使用API 16的平板电脑上运行时,在onActivityResult()
中,resultCode
永远不会是RESULT_OK
,{{1总是 null 。我见过许多解决方案,如调用intent
,但似乎没有任何效果。
任何提示都会有用。感谢。
修改
我已经用调试器分析了两个版本中我的活动内容。这些状态是调用getParent().setResult()
之前Gallery
活动的状态。在API 22上,我有这个:
finish()
在API 16上,我有这个:
mParent = null
mResultCode = -1
mResultData = {Intent@5081} "Intent { (has extras) }"
mAction = null
mCategories = null
mClipData = null
mComponent = null
mContentUserHint = -2
mData = null
mExtras = {Bundle@5134} "Bundle[{photo=[/storage/emulated/0/DCIM/Camera/....jpg, ...]}]"
mFlags = 0
mLaunchParams = {DualScreenLaunchParams@5135} "DualScreenLaunchParams { mScreen=UNKNOWN mFlags=0 }"
mMultiWindowStyle = null
mPackage = null
mSelector = null
mSourceBounds = null
mType = null
mWindowMode = 0
mWindowPosition = null
mWindowScale = 0.0
shadow$_klass_ = {Class@1261} "class android.content.Intent"
shadow$_monitor_ = -1875875432
存在差异,但似乎mParent = null
mResultCode = -1
mResultData = {Intent@830041848224} "Intent { (has extras) }"
mAction = null
mCategories = null
mClipData = null
mComponent = null
mData = null
mExtras = {Bundle@830042231640} "Bundle[{photo=[/storage/sdcard0/Pictures/....jpeg, ...]}]"
mType = null
mPackage = null
mSelector = null
mSourceBounds = null
mFlags = 0
完成了它的工作。
答案 0 :(得分:0)
Finally, I got to the problem. The key was in the doc for startActivityForResult
. It states:
For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result.
I was indeed using singleTask
as a launch mode. I switched to singleTop
and it worked. I am studying launch modes to have everything working out.
答案 1 :(得分:-1)
确保在setResult
finish()