当调用startActivityForResult()

时间:2018-11-23 13:56:20

标签: android crash android-camera-intent android-8.1-oreo

有人来救我。 我正在构建一个需要从设备相机拍摄照片并将缩略图加载到ImageView的android应用程序,请注意,我不想保存此图像,我只想将其加载到ImageView并从中获取可绘制的位图稍后使用ImageView。

这是我实现的: 要启动相机:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(cameraIntent.resolveActivity(activityWeakReference.get().getPackageManager()) != null){
startActivityForResult(cameraIntent, UPLOAD_PICTURE_CAMERA);
}

在调用onActivityResult(int requestCode, int resultCode, Intent data)时调用以下startActivityForResult()

@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上,活动会立即关闭,因为打开了相机,因此不会调用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'

谁在奥利奥(Oreo)上面临同样的问题?我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

我发现我是在设备上启用的称为“杀死后台活动”的用户设置,因此能够弄清楚这一点。我所做的就是取消选中它,并且可以进行呼叫活动来接收来自Camera Intent的结果。

感谢您的帮助。