duanhong169 / Camera

📸 Use Android camera to take pictures and videos, based on `camera2` api.
Apache License 2.0
130 stars 45 forks source link

After captured Image or recorded video #9

Closed mskocabay closed 5 years ago

mskocabay commented 5 years ago

After completed camera operations , I want to show image or video in other actvity . Is that true ?

In PhotographerActivity.java i added to announcingNewFile method these codes. Is it enogh or must i do something about stop camera or backgroundtasks?

Thank you

            ` private void announcingNewFile(String filePath) {
    Toast.makeText(PhotographerActivity.this, "File: " + filePath, Toast.LENGTH_SHORT).show();
    Utils.addMediaToGallery(PhotographerActivity.this, filePath);

    finish();

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    Intent intent = new Intent(PhotographerActivity.this,PreviewActivity.class);
    intent.putExtra("mode",0);
    intent.putExtra("file",filePath);
    startActivity(intent);

}`
duanhong169 commented 5 years ago

The PhotographerActivity.onPause() method stops the camera and background tasks for you:

https://github.com/duanhong169/Camera/blob/d9fd8b4db468fb830d72e9852de629923cb0301a/app/src/main/java/top/defaults/cameraapp/PhotographerActivity.java#L297-L301

So if you have kept the onPause() implementation, then there is no need to do it anymore.

mskocabay commented 5 years ago

Thank you very much , it is best camera2 example. I tried it but returns this error in andoid 6

FATAL EXCEPTION: main Process: arge.aa.com.myapplication, PID: 10109 java.lang.RuntimeException: Unable to pause activity {com.camera.myapplication/arge.aa.com.myapplication.PhotographerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean

and in android 8

FATAL EXCEPTION: main Process: arge.aa.com.myapplication, PID: 19986 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.post(java.lang.Runnable)' on a null object reference at arge.aa.com.myapplication.Camera2Photographer$4.onImageAvailable(Camera2Photographer.java:195) at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:812) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

duanhong169 commented 5 years ago

Seems that the onShotFinished() is called too early, and the image is not finished saving at that time:

https://github.com/duanhong169/Camera/blob/d9fd8b4db468fb830d72e9852de629923cb0301a/camera/src/main/java/top/defaults/camera/Camera2Photographer.java#L961-L967

then when the ImageSaver try to save the image, the backgroundHandler has been set to null by onPause():

https://github.com/duanhong169/Camera/blob/d9fd8b4db468fb830d72e9852de629923cb0301a/camera/src/main/java/top/defaults/camera/Camera2Photographer.java#L185-L193

I think we could try to put the event:

https://github.com/duanhong169/Camera/blob/d9fd8b4db468fb830d72e9852de629923cb0301a/camera/src/main/java/top/defaults/camera/Camera2Photographer.java#L966

after:

https://github.com/duanhong169/Camera/blob/d9fd8b4db468fb830d72e9852de629923cb0301a/camera/src/main/java/top/defaults/camera/Camera2Photographer.java#L190

@mskocabay I'm sorry I have no enough time to debug this issue, could you please try to solve this by your own and it's much better if you can create a pull request if applicable.

mskocabay commented 5 years ago

ok thank you very much