coomar2841 / image-chooser-library

An Easy Image/Video Chooser Library for your Android Apps
646 stars 193 forks source link

Bug to take photo from camera #158

Closed AlexPGA closed 8 years ago

AlexPGA commented 8 years ago

Hi, I'm using the library in my personal application. When I have a problem use . The problem is to take the picture and send it to another activity. If photography is done in portrait, the other activity is not sent . However if it is done in landscape if it works perfectly . How I can do to fix this or force the application to perform in landscape photography ? I use Samsung S4 and its failed

Thanks for your reply

coomar2841 commented 8 years ago

Are you reinitializing the ImageChooserManager correctly on onActivityResult?

Take a look here. https://github.com/coomar2841/image-chooser-library/blob/dev/app/src/main/java/com/beanie/imagechooserapp/ImageChooserActivity.java#L154

AlexPGA commented 8 years ago

yes. I reinitializing the imageChooserManager. When I get a photo, the screen shows the image and two button to accept o discard the photo. Here, if the screen is in portrait, the image is saved in the memory but the intent data no recibes nothing and crash the app. If I save in landscape dont happend this problem

coomar2841 commented 8 years ago

Can you post your code in onActivityResult method?

AlexPGA commented 8 years ago

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == FragmentActivity.RESULT_OK) {
        switch (requestCode) {
            case ChooserType.REQUEST_PICK_PICTURE:
                if (imageChooserManager == null) {
                    reinitializeImageChooser();
                }
                imageChooserManager.submit(requestCode, data);
                break;
            case ChooserType.REQUEST_CAPTURE_PICTURE:
                if (imageChooserManager == null) {
                    reinitializeImageChooser();
                }
                imageChooserManager.submit(requestCode, data);
                break;
        }
    }
}
coomar2841 commented 8 years ago

In the re-initialize method, can you check if the path you are passing is null? It shouldn't be null.

AlexPGA commented 8 years ago

It isnt null. My app is a fragment in an activity and inside the fragment I have a button to pick a photo from camera o from gallery. When selected camera, take photo and save, it crash or close activity parent. Can you send me and example using your library and activity with fragments? thanks for yours replys

coomar2841 commented 8 years ago

https://github.com/coomar2841/image-chooser-library/blob/dev/app/src/main/java/com/beanie/imagechooserapp/FragmentImageChooserActivity.java

https://github.com/coomar2841/image-chooser-library/blob/dev/app/src/main/java/com/beanie/imagechooserapp/fragments/ImageChooserFragment.java

AlexPGA commented 8 years ago

thanks for your help. The problem was in the software of camera of my Samsung. Now, all functions correctly. Thanks

unxavi commented 8 years ago

@AlexPGA What do you mean the "The problem was in the software of camera of my Samsung"?

I am having the same issue.

unxavi commented 8 years ago

@coomar2841 by the way, i try with the sample app in google play, with a Samsung Galaxy S4. With the fragment example, your app crash. :S

coomar2841 commented 8 years ago

Checking that.

Thanks and Regards,Kumar Bibek

http://techdroid.kbeanie.com http://techdroid.kbeanie.comhttp://www.kbeanie.com http://www.kbeanie.com

On Thu, Mar 3, 2016 at 4:54 PM, Javier Vieira notifications@github.com wrote:

@coomar2841 https://github.com/coomar2841 by the way, i try with the sample app in google play, with a Samsung Galaxy S4. With the fragment example, your app crash. :S

— Reply to this email directly or view it on GitHub https://github.com/coomar2841/image-chooser-library/issues/158#issuecomment-191715260 .

AlexPGA commented 8 years ago

I installed a software for the camera from google play and it worked perfectly . My Samsung has installed one room and the camera software was the problem

unxavi commented 8 years ago

@coomar2841

I think the camera from the S4 make rotate the fragment/activity, and when this happens the fragment is not really attach to an activity, and the problem comes when onImageChose wants to execute:

03-03 12:40:12.096 3543-7750/com.icodehigh.myapp E/ImageProcessorThread: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference at com.icodehigh.myapp.ui.authorization.AuthorizationActivityFragment.onImageChosen(AuthorizationActivityFragment.java:326) at com.kbeanie.imagechooser.api.ImageChooserManager.onProcessedImage(ImageChooserManager.java:339) at com.kbeanie.imagechooser.threads.ImageProcessorThread.processingDone(ImageProcessorThread.java:187) at com.kbeanie.imagechooser.threads.ImageProcessorThread.process(ImageProcessorThread.java:163) at com.kbeanie.imagechooser.threads.ImageProcessorThread.processImage(ImageProcessorThread.java:154) at com.kbeanie.imagechooser.threads.ImageProcessorThread.run(ImageProcessorThread.java:62) 03-03 12:40:12.161 3543-3543/com.icodehigh.myapp D/Activity: performCreate Call secproduct feature valuefalse 03-03 12:40:12.161 3543-3543/com.icodehigh.myapp D/Activity: performCreate Call debug elastic valuetrue 03-03 12:40:12.281 3543-3553/com.icodehigh.myapp W/art: Suspending all threads took: 23.085ms 03-03 12:40:12.281 3543-3543/com.icodehigh.myapp E/ViewRootImpl: sendUserActionEvent() mView == null 03-03 12:40:12.351 3543-7750/com.icodehigh.myapp E/AndroidRuntime: FATAL EXCEPTION: Thread-2541 Process: com.icodehigh.myapp, PID: 3543 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference at com.icodehigh.myapp.ui.authorization.AuthorizationActivityFragment.onError(AuthorizationActivityFragment.java:341) at com.kbeanie.imagechooser.api.ImageChooserManager.onError(ImageChooserManager.java:346) at com.kbeanie.imagechooser.threads.ImageProcessorThread.run(ImageProcessorThread.java:72) 03-03 12:40:12.506 3543-3543/com.icodehigh.myapp E/ViewRootImpl: sendUserActionEvent() mView == null

AlexPGA commented 8 years ago

yes. When camera rotate image, fragment is destroy. My problem solves when use another app to camera

unxavi commented 8 years ago

@coomar2841 Hi @AlexPGA is right, if you use the stock camera app from Samsung Galaxy 4, then the library breaks, because that APP makes the screen to rotate and recreate the fragment.

If you user another camera APP with the same phone, the library works

unxavi commented 8 years ago

@coomar2841 do you have any idea or work around not to use another camera APP? because is hard to accept this as an answer if the users of the app is the general public :s

coomar2841 commented 8 years ago

You can try delaying the execution till the activity and fragment is re-initialized properly.

unxavi commented 8 years ago

@coomar2841 do you mind to provide a little example? I would really apreciate it :dancers: I dont get clear where to delay the execution and how :weary:

I am just doing it like your fragment example:

https://github.com/coomar2841/image-chooser-library/blob/dev/app/src/main/java/com/beanie/imagechooserapp/fragments/ImageChooserFragment.java

coomar2841 commented 8 years ago
@Override
    public void onImageChosen(final ChosenImage image) {
        if (getActivity() == null) {
            getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    onImageChosen(image);
                }
            }, 1000);
        } else {
            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    pbar.setVisibility(View.GONE);
                    if (image != null) {
                        textViewFile.setText(image.getFilePathOriginal());
                        imageViewThumbnail.setImageURI(Uri.parse(new File(image
                                .getFileThumbnail()).toString()));
                        imageViewThumbSmall.setImageURI(Uri.parse(new File(image
                                .getFileThumbnailSmall()).toString()));
                    }
                }
            });
        }
    }

Try this snippet once. But, this is not a good solution. However, if you can confirm that this works, I can think of some workarounds.

unxavi commented 8 years ago

No, it seem to break because "getView" returns null

03-03 14:02:23.531 5284-6972/com.icodehigh.myapp E/ImageProcessorThread: Attempt to invoke virtual method 'boolean android.view.View.postDelayed(java.lang.Runnable, long)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.View.postDelayed(java.lang.Runnable, long)' on a null object reference at com.icodehigh.myapp.ui.authorization.AuthorizationActivityFragment.onImageChosen(AuthorizationActivityFragment.java:326) at com.kbeanie.imagechooser.api.ImageChooserManager.onProcessedImage(ImageChooserManager.java:339) at com.kbeanie.imagechooser.threads.ImageProcessorThread.processingDone(ImageProcessorThread.java:187) at com.kbeanie.imagechooser.threads.ImageProcessorThread.process(ImageProcessorThread.java:163) at com.kbeanie.imagechooser.threads.ImageProcessorThread.processImage(ImageProcessorThread.java:154) at com.kbeanie.imagechooser.threads.ImageProcessorThread.run(ImageProcessorThread.java:62)

this line "AuthorizationActivityFragment.java:326" is "getView().postDelayed(new Runnable() {"

Thanks a lot for your time, I hope we can find a solution :D

coomar2841 commented 8 years ago

Sorry. That was stupid of me. That wouldn't have worked. Give me a few hours. Will get back.

Sent from OnePlusOne. On 03-Mar-2016 6:34 pm, "Javier Vieira" notifications@github.com wrote:

No, it seem to break because "getView" returns null

03-03 14:02:23.531 5284-6972/com.icodehigh.myapp E/ImageProcessorThread: Attempt to invoke virtual method 'boolean android.view.View.postDelayed(java.lang.Runnable, long)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.View.postDelayed(java.lang.Runnable, long)' on a null object reference at com.icodehigh.myapp.ui.authorization.AuthorizationActivityFragment.onImageChosen(AuthorizationActivityFragment.java:326) at com.kbeanie.imagechooser.api.ImageChooserManager.onProcessedImage(ImageChooserManager.java:339) at com.kbeanie.imagechooser.threads.ImageProcessorThread.processingDone(ImageProcessorThread.java:187) at com.kbeanie.imagechooser.threads.ImageProcessorThread.process(ImageProcessorThread.java:163) at com.kbeanie.imagechooser.threads.ImageProcessorThread.processImage(ImageProcessorThread.java:154) at com.kbeanie.imagechooser.threads.ImageProcessorThread.run(ImageProcessorThread.java:62)

this line "AuthorizationActivityFragment.java:326" is "getView().postDelayed(new Runnable() {"

Thanks a lot for your time, I hope we can find a solution :D

— Reply to this email directly or view it on GitHub https://github.com/coomar2841/image-chooser-library/issues/158#issuecomment-191752808 .

coomar2841 commented 8 years ago

Can you guys give this APK a try and let me know. If it doesn't work, please send me the logs.

https://www.dropbox.com/s/37j486wq4tivt8h/icl-samsung-s4-problem.apk?dl=1

unxavi commented 8 years ago

@coomar2841

I have download and try several times, I really believe that the first 2 or 3 times i try all was fine. No crash and the picture taken by the camera was show. Then i close the app and open again and it fails. No crash but the image is not loaded. I reinstall the app and nothing, no crash but neither the image is show.

PD: I filtered the logs with "com.beanie.imagechooserapp"

cold start fail.txt image show.txt

coomar2841 commented 8 years ago

Ok, At least the crash is not happening. From the logs, I see nothing suspicious. Could you try to determine the exact steps which can reproduce this problem every time. I am quite sure there's a specific scenario where it is failing.

unxavi commented 8 years ago

in my code, the problem is that with the camera of samsung in the S4, the method "onImageChosen" "getActivity" is always null. And the fragment gets recreated twice... i think is because the camera app forces the screen rotation in the app. I will try to make a video for you with your app.

coomar2841 commented 8 years ago

I have handled that as well. Check the latest code of ImageChooserFragment.

unxavi commented 8 years ago

OK, I have made the video. I see in the video that you can not see very well the rotation that the camera makes, but if you see the notification bar, you can see how in some moments before returning to the app is in landscape. Also in the video you can see how with another camera software it actually works.

Hope this can help.

PD1: When in the video, the camera looks to be in landscape, in real life is in portrait mode. PD2: I had to make a ZIP because the comments does not support mp4

icl-samsungS4-cameraProblem.zip

unxavi commented 8 years ago

@coomar2841 besides the video I post, so far the only think i can see is that the fragment gets recreated twice and "onImageChosen" gets executed twice.

The line,

imageViewThumbnail.setImageURI(Uri.parse(new File(image.getFileThumbnail()).toString()));

Gets executed, but the image is never show in the thumbnail

coomar2841 commented 8 years ago

Can you check if the file object has a thumbnail path? I just hate Samsung...

unxavi commented 8 years ago

Hey mate!

I have found out new info.

The file object has the path, it does and it's fine. In fact if I use picasso to tell to load an image from the internet when the "run" is executed, it wont show the image with picasso. So the thing does not seem to do with the load file, neither the path.

I log out the call of the file object and this is the result:

D/AuthorizationFragment: run() file path: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg D/AuthorizationFragment: run(): file getAbsolutePath: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg D/AuthorizationFragment: run(): file getPath: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg D/AuthorizationFragment: run(): file getCanonicalPath: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg

Now i find interesting that when I log the lifecycle after using the camera APP I think is a bit weird:

1) Open the app and the fragment where I am going to choose the picture. The fragment call the following methods (so far is normal):

AuthorizationFragment: onAttach() AuthorizationFragment: onCreateView() AuthorizationFragment: onActivityCreated()

2) I tell the app to take a picture. (so far is normal):

AuthorizationFragment: showImageChooser() AuthorizationFragment: takePicture() AuthorizationFragment: onSaveInstanceState()

3) I come back from the camera APP to the fragment, the lifecycle ocurrs twice, I dont see that very normal. Actually after called "onImageChosen" it calls again all the fragment creation lifecycle. how do you see this?

D/AuthorizationFragment: onAttach() called D/AuthorizationFragment: onCreateView() called D/AuthorizationFragment: onActivityCreated() called D/AuthorizationFragment: onActivityResult() called D/AuthorizationFragment: onSaveInstanceState() called D/AuthorizationFragment: onImageChosen() called D/AuthorizationFragment: onAttach() called D/AuthorizationFragment: onCreateView() called D/AuthorizationFragment: onActivityCreated() called D/AuthorizationFragment: run() called D/AuthorizationFragment: run() file path: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg D/AuthorizationFragment: run(): file getAbsolutePath: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg D/AuthorizationFragment: run(): file getPath: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg D/AuthorizationFragment: run(): file getCanonicalPath: /storage/emulated/0/bichooser/1457354826742_fact_1.jpg

finally I have found something out, if we make the fragment with the setRetainInstance(true), then it works, I guess it works because it nevers destroy the fragment

coomar2841 commented 8 years ago

This shouldn't happen actually, and that's what is weird with the Camera App. setRetainInstance would work.. But it's not recommended.

unxavi commented 8 years ago

@coomar2841 yes, I know setRetainInstance(true) is not recommended :-1: but so far is the only way i have found to make it work :S