Activity gets killed while executing the camera in

2020-07-10 19:59发布

[EDIT: Problem solved. Please see my answer below.]

In my app I call the system camera to take a picture, and then handle the result in onActivityResult. You know, the usual. It used to work, but now my calling activity gets killed while I'm taking the picture. Specifically, onDestroy() is called on my activity right after I press the camera shutter. The photo does get taken & saved (I've checked that the file gets written on the SD card). After I accept the photo, instead of returning to the calling activity and invoking onActivityResult, the previous activity in the activity stack gets called. I see no exceptions in the logcat. My custom exception handler doesn't get called. If it matters, my app also includes a service that listens to GPS updates, but I unregister all the receivers in onPause().

Here's the call stack for MyCallingActivity.onDestroy():

Thread [<1> main] (Suspended (breakpoint at line 303 in NewPlaceDetailsActivity))   
    NewPlaceDetailsActivity.onDestroy() line: 303   
    ActivityThread.performDestroyActivity(IBinder, boolean, int, boolean) line: 2663    
    ActivityThread.handleDestroyActivity(IBinder, boolean, int, boolean) line: 2694 
    ActivityThread.access$2100(ActivityThread, IBinder, boolean, int, boolean) line: 117    
    BinderProxy(ActivityThread$H).handleMessage(Message) line: 968  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 130 
    ActivityThread.main(String[]) line: 3687    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 507  
    ZygoteInit$MethodAndArgsCaller.run() line: 842  
    ZygoteInit.main(String[]) line: 600 
    NativeStart.main(String[]) line: not available [native method]  

This is how I start the camera activity, in case you're wondering:

protected void startCamera() {
    createPhotoDirsIfNeeded();

    String fileName = "temp.jpg";  
    ContentValues values = new ContentValues();  
    values.put(MediaStore.Images.Media.TITLE, fileName);  
    m_capturedImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

    m_photoFileName = APP_PHOTO_PATH + "/" + DateFormat.format(DATE_FORMAT, Calendar.getInstance().getTime()) + ".jpg";
    File picFile = new File(m_photoFileName);
    if(picFile.exists()) {
        picFile.delete();
    }

    // start the camera activity
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,  Uri.fromFile(picFile));
    startActivityForResult(intent, IntentHelper.REQUEST_TAKE_PHOTO);
}

How can I find out why does my activity get killed, AND removed from the stack instead of being created again?

3条回答
The star\"
2楼-- · 2020-07-10 20:20

I guess the problem is 1) in your custom implementation of exception handler and 2) this line:

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile));
  1. Your exception handler implementation doesn't allow you to see the NullPointerException which is thrown in onActivityResult method when you try to access the data from the Intent returned.
  2. It seems to be a bug which prevents Intent from being passed to onActivityResult method if an output file has been put as an extra to the Intent starting camera Activity.

Try removing the line and see if your Activity is recreated when camera Activity has been finished.

查看更多
Anthone
3楼-- · 2020-07-10 20:22

OK, I found the problem. The issue was not so much that the activity was being killed, but that it was not being restored. I realized that this can happen when your activity is declared in the manifest with android:noHistory="true". So the activity was removed from the stack upon being killed, and the previous activity was shown instead.

After I removed the noHistory parameter, the activity started appearing again after using the camera.

查看更多
男人必须洒脱
4楼-- · 2020-07-10 20:44

Please follow this steps:

1)  Add this in manifest :
    <uses-feature android:name="android.hardware.camera"></uses-feature>

2)  startCamera() :
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,  Uri.fromFile(picFile));
    startActivityForResult(intent, IntentHelper.REQUEST_TAKE_PHOTO);


3)  if (requestCode == PICTURE_RESULT) //
             if (resultCode == Activity.RESULT_OK) {
                // Display image received on the view
                 Bundle b = data.getExtras(); // Kept as a Bundle to check for other  things in my actual code
                 Bitmap pic = (Bitmap) b.get("data");

                 if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
                     pictureHolder = (ImageView) this.findViewById(R.id.IMAGE);
                     pictureHolder.setImageBitmap(pic);
                     pictureHolder.invalidate();
                 }
             }
             else if (resultCode == Activity.RESULT_CANCELED) {...}
    }

If still this code doesn't work for you, please refer to following links :

查看更多
登录 后发表回答