react-native-image-crop-picker icon indicating copy to clipboard operation
react-native-image-crop-picker copied to clipboard

When taking a photo from the camera, causing the app to restart

Open ShuChangSC opened this issue 6 years ago • 47 comments

Version

Tell us which versions you are using:

  • react-native-image-crop-picker v0.23.0
  • react-native v0.55.4

Platform

  • Android

Expected behaviour

Take a picture from the camera and display it

Actual behaviour

When taking a photo from the camera, causing the app to restart

Steps to reproduce

takeByCamera = () => { ImagePicker.openCamera({ width: 400, height: 400, }) .then(image => { console.log(image); this.setState({ avatarPath: {uri: image.path} }) }) .catch(error => { console.log(error) }) };

Attachments

rRestart the app frequently,but sometimes it is ok

ShuChangSC avatar Mar 13 '19 08:03 ShuChangSC

I am facing same issue.

divyamantri1292 avatar Mar 27 '19 11:03 divyamantri1292

I am also facing the same issue. Moto X4, android v9

Also restarts app like 90% of the time, but sometimes its ok. I've tried decreasing compressed size and quality to try and help if its a memory thing, but that did nothing.

krewllobster avatar Mar 27 '19 16:03 krewllobster

Same thing seems like the camera view is closing the app. Can you guys see if not taking a picture and going back, our app is closed or restarting?

filipef101 avatar Mar 28 '19 13:03 filipef101

probably duplicate of https://github.com/ivpusic/react-native-image-crop-picker/issues/836

filipef101 avatar Mar 28 '19 13:03 filipef101

@filipef101 yes, it also sometimes crashes when attempting to use the android back-button to go back to the main app. This seems like a wide-spread issue over many versions of the library and over many versions of Android operating systems. The earliest issue I could find was #240 (crash after photo confirm on android) from Feb 2017. This thread now is the most recent. Other issue threads, all of which are still open are:

  1. #240
  2. #378
  3. #501 (maybe related)
  4. #505
  5. #519
  6. #648 (maybe related)
  7. #822 (maybe related)
  8. #836
  9. #900

krewllobster avatar Mar 28 '19 15:03 krewllobster

Basically the issue is that the Camera Screen is an independent screen that then passes the data to JS, this is my assumsion. This probably can be solved by using other lib to take the photo then use this on to crop it. If I face this issue on my app I'll share the solution here. As so far I only noticed this on debug

filipef101 avatar Mar 28 '19 16:03 filipef101

Here's the memory profile as the camera opens:

Screen Shot 2019-03-28 at 12 18 15 PM

It just tanks down to zero, and I don't think that it was over-using before? Here's the full session profile, I'm not experienced enough to know if it looks ok or not:

Screen Shot 2019-03-28 at 12 17 33 PM

krewllobster avatar Mar 28 '19 16:03 krewllobster

@filipef101 Also, I came to this library after I started getting this problem with react-native-image-picker.

The only other library I can find is react-native-camera, which I guess creates a camera view as a react component, rather than opening the camera intent over the app? If it does work like that, do you think it would solve this issue?

krewllobster avatar Mar 28 '19 16:03 krewllobster

Yup, probably does, I never used that one but used but did use react-native-camera-kit to implement my custom camera view (like a year ago, had a few issues with it and needed to fork it)

But react-native-camera certainly doesn't have this issue so you can pass the image it gives you and use this library to crop it. It's a matter of making that seamless

filipef101 avatar Mar 28 '19 16:03 filipef101

Also so far I don't face this on my release, might be only happening if you use too much memory

filipef101 avatar Mar 28 '19 16:03 filipef101

Yeah, I'm spending today cleaning up my code-base. Uninstalling unused dependencies, making sure I'm only requiring what I need from libs, etc. We'll see if that helps at all. If it doesn't I'm going to be super disappointed, but it needs to be done regardless.

krewllobster avatar Mar 28 '19 20:03 krewllobster

@ivpusic @filipef101 The strange thing is that the camera activity takes enough memory to force the OS to kill the RN Activity. The RN Activity is saved -> stopped -> destroyed, and when the camera activity tries to pass information back to the RN Activity, rather than starting from a saved state, its starting from a fresh state.

Is this potentially something in my mainActivity.java file where I don't have the appropriate onStop() and onStart() handlers that should be saving the app state? Again, not an Android dev here, but something fishy is going on.

In issue #519, @pisacode mentioned that he was using react-native-splash-screen. He said that he fixed the issue by removing it. He didn't specify however what "removing it" meant...is it a complete uninstall? The recommended setup for RNSplashScreen is to have a SplashActivity.java file that looks like this:

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        startActivity(intent);
        finish();
    }
}

So then my MainActivity.java looks like this:

public class MainActivity extends ReactActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this);
        super.onCreate(savedInstanceState);
    }
    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "***myPackageName***";
    }
}

Is it not receiving a savedInstanceState properly? I have no onStop() or onPause() methods on my MainApplication.java class.

krewllobster avatar Mar 28 '19 22:03 krewllobster

It happens in some devices when you open the camera, the camera request more RAM therefore closing all the the apps(it do not crash the app), in order to reproduce the problem there is the need to enable in the developer tools the option called "Don't keep activities".

The fix in my case was very simple, just the persist the state of my app using redux-persist.

osdamv avatar Apr 01 '19 18:04 osdamv

I also suffered all the day and found it doesn't happen in the release apk. I am using react-native v0.59.1 and react-native-image-crop-picker v0.23.1. Please build the apk with gradlew assembleRelease in terminal, not exporting wizard in Android Studio. Hope it helps.

ostar0816 avatar Apr 10 '19 02:04 ostar0816

I'm having the same issue. The issue doesn't happen when there are no other apps consuming memory.

Version

"react-native-image-crop-picker": "^0.24.1"
"react-native": "0.58.5"

Platform

Android

fernandofranca avatar Jun 12 '19 21:06 fernandofranca

Hi. I did a lot of digging after coming across this issue across some phones with extremely aggressive backgrounding (see here for a list).

After various attempts at all kinds of solutions (e.g. foreground service, react-native-background-timer, etc.), I found that the best solution was offered by @osdamv where we persist navigation state (and possibly redux state) and rehydrate the app when it returns from the camera. I recommend testing with "Don't keep activities" via the Android developer tools.

Unfortunately, for a potential fix for the library itself, I can't confidently say it can be done. Most of the research I've done on Android usually reverts back to saving instance state anyway.

If there's enough demand I might help out with an example on how to persist. It's really not that far from just installing the persist libraries (for react-navigation and redux-persist) though.

barbsicle avatar Jul 25 '19 12:07 barbsicle

I think one of possible solution could be not use such kind of libraries (like react-native-image-crop-picker, react-native-image-picker), but implement camera/library picking interface using react-native-camera & CameraRoll

Strate avatar Oct 17 '19 12:10 Strate

App restart on mi device when taking pic from camera.

rmamtaventures avatar Oct 22 '19 12:10 rmamtaventures

Yes,right,i develop on Redmi device.

| | idealchang 邮箱:[email protected] |

Signature is customized by Netease Mail Master

On 10/22/2019 20:11, rmamtaventures wrote:

App restart on mi device when taking pic from camera.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

ShuChangSC avatar Oct 22 '19 12:10 ShuChangSC

Any solution of app restart?

rmamtaventures avatar Nov 04 '19 08:11 rmamtaventures

These settings are already done. but not working on redmi. Any update?

rmamtaventures avatar Nov 20 '19 10:11 rmamtaventures

Any update on app restart issue?

rmamtaventures avatar Dec 06 '19 12:12 rmamtaventures

Any update on app restart issue?

viv3kk avatar Jan 10 '20 05:01 viv3kk

Any update on app restart issue?

engineerahkhani avatar Feb 02 '20 09:02 engineerahkhani

any update?

Hamawis avatar Feb 17 '20 09:02 Hamawis

Guys, I managed to solve by defining a smaller size in the target picture and enable the crop. In fact, the images were too large when it comes to sizes (over 3000x3000 px). Here is how I'm calling the camera on Android devices.

ImagePicker.openCamera({ width: 500, height: 400, cropping: true, compressImageQuality: 0.4, }).then(image => { //do some processing } });

FranklinSilva avatar May 28 '20 17:05 FranklinSilva

doesn't work on reduce image size ! still restart the app after click "OK" Realme X (6GB + 64GB) on Android 10

12343954 avatar Jun 26 '20 23:06 12343954

The restart problem also appeared in Huawei P20 Pro

kluas11 avatar Jul 04 '20 09:07 kluas11

I fixed this by asking the permissions on Android and iOS (using react-native-permissions lib):

useEffect(() => {
    async function askPermissions() {
        if (Platform.OS === 'android') {
            await Permissions.requestMultiple([
                'android.permission.CAMERA',
                'android.permission.WRITE_EXTERNAL_STORAGE',
            ]);
        }

        if (Platform.OS === 'ios') {
            await Permissions.requestMultiple(['ios.permission.CAMERA', 'ios.permission.PHOTO_LIBRARY']);
        }
    }

    askPermissions();
}, []);

victorwpbastos avatar Jul 08 '20 01:07 victorwpbastos

The problem is not the permissions, because the restart happens after opening the camera, that is, the permission has already been granted.

douglasjunior avatar Jul 08 '20 11:07 douglasjunior