Android 8.1 在调用 startActivityForResult() 时关闭 Activity
Android 8.1 Closes Activity when startActivityForResult() is called
有人来救我了。
我正在构建一个 android 应用程序,它需要从设备相机拍摄照片并将缩略图加载到 ImageView 中,请注意,我不想保存此图像,我只想将它加载到 ImageView 中并获取稍后可从 ImageView 绘制位图。
这是我实现的:
要启动相机:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(activityWeakReference.get().getPackageManager()) != null){
startActivityForResult(cameraIntent, UPLOAD_PICTURE_CAMERA);
}
下面是调用startActivityForResult()
时调用的onActivityResult(int requestCode, int resultCode, Intent data)
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == UPLOAD_PICTURE_CAMERA) {
try{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageViewphoto.setImageBitmap(imageBitmap);
}catch (Exception e){
Log.e(TAG, Utility.stringify(e));
}
}
问题:
上面的代码适用于 android 7.0,但在 Oreo 8.1 上,activity 会立即关闭相机,因此不会调用 onActivityResult()
。
这是我能得到的所有日志:
I/PhoneWindow: isNeedChangeStatusBarColor taskInfo: [android.app.ActivityManager$RunningTaskInfo@c7a24ee] size: 1
isAPPNeedChangeSBColor pkgName: com.a3lineng.softwaredev.freedom_app needKeep: false
isNeedChangeStatusBarColor false
I/PhoneWindow: isNeedChangeNaviBarColor taskInfo: [android.app.ActivityManager$RunningTaskInfo@5566d8f] size: 1
I/PhoneWindow: isAPPNeedChange pkgName: com.a3lineng.softwaredev.freedom_app needKeep: false
isNeedChangeNaviBarColor false
generateLayout mNavigationBarColor: ff000000
I/PhoneWindow: generateLayout isLightNavi false, Visibility: 0
I/zygote: Do full code cache collection, code=505KB, data=398KB
I/zygote: After code cache collection, code=495KB, data=332KB
D/BaseActivity: On Pause is called
I/zygote: Do partial code cache collection, code=505KB, data=341KB
I/zygote: After code cache collection, code=505KB, data=341KB
Increasing code cache capacity to 2MB
D/BaseActivity: On Stop is called
Disconnected from the target VM, address: 'localhost:8857', transport: 'socket'
谁在吃奥利奥时遇到同样的问题?如有任何建议,我们将不胜感激。
在我的案例中,我已经能够解决这个问题,我发现这是在设备上启用了一个名为 Kill Background Activities 的用户设置。我所做的只是取消选中它,调用 activity 可用于接收来自相机意图的结果。
感谢您的帮助。
有人来救我了。 我正在构建一个 android 应用程序,它需要从设备相机拍摄照片并将缩略图加载到 ImageView 中,请注意,我不想保存此图像,我只想将它加载到 ImageView 中并获取稍后可从 ImageView 绘制位图。
这是我实现的: 要启动相机:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(activityWeakReference.get().getPackageManager()) != null){
startActivityForResult(cameraIntent, UPLOAD_PICTURE_CAMERA);
}
下面是调用startActivityForResult()
时调用的onActivityResult(int requestCode, int resultCode, Intent data)
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == UPLOAD_PICTURE_CAMERA) {
try{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageViewphoto.setImageBitmap(imageBitmap);
}catch (Exception e){
Log.e(TAG, Utility.stringify(e));
}
}
问题:
上面的代码适用于 android 7.0,但在 Oreo 8.1 上,activity 会立即关闭相机,因此不会调用 onActivityResult()
。
这是我能得到的所有日志:
I/PhoneWindow: isNeedChangeStatusBarColor taskInfo: [android.app.ActivityManager$RunningTaskInfo@c7a24ee] size: 1
isAPPNeedChangeSBColor pkgName: com.a3lineng.softwaredev.freedom_app needKeep: false
isNeedChangeStatusBarColor false
I/PhoneWindow: isNeedChangeNaviBarColor taskInfo: [android.app.ActivityManager$RunningTaskInfo@5566d8f] size: 1
I/PhoneWindow: isAPPNeedChange pkgName: com.a3lineng.softwaredev.freedom_app needKeep: false
isNeedChangeNaviBarColor false
generateLayout mNavigationBarColor: ff000000
I/PhoneWindow: generateLayout isLightNavi false, Visibility: 0
I/zygote: Do full code cache collection, code=505KB, data=398KB
I/zygote: After code cache collection, code=495KB, data=332KB
D/BaseActivity: On Pause is called
I/zygote: Do partial code cache collection, code=505KB, data=341KB
I/zygote: After code cache collection, code=505KB, data=341KB
Increasing code cache capacity to 2MB
D/BaseActivity: On Stop is called
Disconnected from the target VM, address: 'localhost:8857', transport: 'socket'
谁在吃奥利奥时遇到同样的问题?如有任何建议,我们将不胜感激。
在我的案例中,我已经能够解决这个问题,我发现这是在设备上启用了一个名为 Kill Background Activities 的用户设置。我所做的只是取消选中它,调用 activity 可用于接收来自相机意图的结果。
感谢您的帮助。