Search code examples
androidcrashandroid-camera-intentandroid-8.1-oreo

Android 8.1 Closes Activity when startActivityForResult() is called


somebody come to my rescue. I am building an android application that require taking a picture from the device camera and loading the thumbnail into an ImageView, note that I don't want to save this image, I only want to load it into an ImageView and get bitmap drawable from the ImageView later.

This is what I have Implemented: To launch the camera:

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

Below is onActivityResult(int requestCode, int resultCode, Intent data) invoked when startActivityForResult() is called:

@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));
                }
}

The Problem: The above code works well up to android 7.0, but on Oreo 8.1, the activity closes immediately the Camera is opened so onActivityResult() is not called.

This is all the log I could get:

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'

Who is facing the same issue on Oreo?, any advice would be appreciated.


Solution

  • I have been able to figure this out in my case i discovered it is a user settings called Kill Background Activities that was enabled on the device. All i did was to uncheck it and the calling activity was available to receive the result from the Camera intent.

    Thanks for helping.