Android: Camera Causes Application To Restart

Hello!

I got problems using the camera with Corona.

After taking a picture, sometimes the app is being restarted (including the Corona Trial popup) instead of showing the most recent screen.

I have already read the thread here. I have NOT activated a developer option “dont keep activity (destroy all activity…)”.

In fact my phone does not provide this option at all (and I guess it is not activated by default).

I had the behaviour with my own app, so I tried the Camera Sample App, and I could reproduce it there as well.

The problem occured most often under the following circumstances:

  • I installed the app from an apk-File and immediately pressed “Open” after installation to start the app.
  • I took the picture and immediately pressed “Finish” when the picture was ready.

My Phone is a HTC Desire S.

I searched the ADB Logcat for hints on the problem. This is not easy due to tons of messages.

But I have seen a message from the ActivityManager, probably related to this issue, saying

“Checking to restart com.ansca.corona.CoronaActivity: changed=0x0, handles=0x4a0”

I can search the log but don’t know exactly what to look for. There did not seem to be any more obvious error messages.

Thanks for your help with this nasty problem!

One more thing:

I always shot the photos at highest Quality (5M).

Your device might be running out of memory when taking high res camera shots, forcing Android to force quit apps running in the background.  Have a look at the Android log via “adb logcat” or “ddms” to see if this is happening.

You can also try requesting the Android OS to give your app a larger heap (ie: more memory) by adding the following setting to your “build.settings” file.  Note that typically only Android 4.0 or higher devices respect this setting.

settings = { android = { largeHeap = true, } }

Thanx for your help!

I tried the “largeHeap” setting, but it did not change the behaviour. This is probably due to the fact my device is only running Android 2.3.5.

I searched the logcat output intensely. I could not find any hints that were obviously related to the issue besides the line I already noted in my first post.

But I must admit that it is very difficult to analyze and understand the whole logcat output without deep knowledge of the Android OS.

Because I could reproduce the problem with the camera sample app, I think there must be other people that encountered this issue too. If it only happens on very specific and rare device / OS configurations, I can live with it.

But if it happens on many devices (especially if they are only a few years old and use the highest camera resolution, which is a common usecase in my opinion), this is quite a dealbreaker for apps using the camera!

One more thing:

I always shot the photos at highest Quality (5M).

I have older devices than yours, such as a 1st generation Droid and an HTC Evo 4G which are both running Android 2.2, and they don’t have this issue.  The camera sample app works fine on them.

Does rebooting your phone help any?  (I’m hoping it will close all other apps and free up memory.)

Or perhaps your device is out of storage space?

One more thing.  You said you sometimes see a Corona “Trial” popup when displaying your app.  The newest release version of Corona, build #1076", should no longer displays a trial popup.  So, have you tried upgrading to the newest release version?

Storage space is probably not the problem. My internal space is above 400 MB, external over 5 Gigs.

I furthermore downloaded the most recent version of the Corona SDK.

This did not fix the problem, but introduced a new issue with the ScrollView. The “takeFocus(event)” function of the ScrollView seems to be “nil” now. The same code was working before. Have there been any changes?

But rebooting the device really did help! Afterwards I could not provoke the error anymore.

But I guess the end-user will not be happy with this “workaround”. Are there other possibilities, or shall we cancel this issue as too rare to be dealt with?

If rebooting solved the problem, then it sounds like you had too many apps running in the background eating up system memory.  What likely happened was that displaying the camera activity window forced other apps that were running in the background to be shutdown, and unfortunately, your app was one of them.  I don’t think we can do anything about that.  Android does not give us permission to force quit other apps running in the background because that would be a security issue.  Also, Corona frees up as much memory as possible by unloading all textures/images before suspending itself to prevent Android from force quitting the app.  That’s as much as we can do.  I will say that this is the first time I’ve heard of this happening, so I hope that makes this a very rare case.

The only other thing I can offer is instead of displaying the device’s default camera app for taking pictures, you can use Corona’s built-in camera activity instead.  You can do this by *removing* the “WRITE_EXTERNAL_STORAGE” permission, but you still have to set the “CAMERA” permission.  Corona’s camera activity is very basic though because it was meant to be used as a fallback mechanism in case a camera app could not be found on the device.

Anyways, I hope this helps.

I have a similar issue on my Samsung S2, and my clients S3.  I have added the code “largeHeap = true” and I will let you know if this helps…

It is very important for me to understand the cause for this problem. Your explanation seems quite feasible.

I have tried Corona’s built-in camera app, and first tests went very well. There were no more unexpected restarts. The user can take pictures very easily. Does it work on iOS the same?

Disadvantages are that users cannote “retake” a picture (not a big deal for my app), and the camera preview seemed a bit distorted (but the final pictures were not).

Even an advantage for my app is the fact that pictures taken with the Corona camera-app are not included in Android’s gallery (which was kind of annoying with the native camera-app).

Thanx for this info, I did not know that there even was a built-in camera app. I think a description to this fact would be an important addition for the API-documentation of “media.show”.

Come to think of it, I think setting largeHeap to true might make it worse, because if the device is running low on memory, then the OS is more likely to quit apps that use the most memory on the device first.  Although I’m shocked that the operating system would decide to take out apps that belong to the same “task” as the app that launched it.

I did a quick Internet search about this issue and discovered that this is unfortunately a common issue on Android…

http://stackoverflow.com/questions/7473061/startactivityforresult-but-not-on-my-apps-activity-stack

http://stackoverflow.com/questions/10910227/startactivityforresult-in-low-memory-situations?rq=1

Do you happen to have a lot of other things loaded in memory such as audio?  If so, then perhaps you should dispose of your audio data when your app is being suspended so that Android is less likely to take out your app during these memory starved situations.

http://docs.coronalabs.com/api/library/audio/dispose.html

You may also want to call Lua’s collectgarbage() function to ensure all unreferenced Lua objects get collected as well.

Other than that, I’m not sure what else can be done since this is an out-of-memory issue.  The reason displaying Corona’s internal Corona camera activity works around this problem is because it belongs to your app.  So, that’s the only guaranteed way around this, but unfortunately I don’t think we can detect these out-of-memory situations until it’s too late.  Hmm…

Hi!

Understanding the problem is the first step in solving it… :slight_smile:

I have no audio in my app yet. There may come some sound effects, but probably not many of them.

What I have in memory are images, because I crop the pictures shot with the camera (downscaled) and show them in a scrollView.

I will do further testing with the Corona-camera app, but it seems ok at first glance.

Thanx for your help!

Your device might be running out of memory when taking high res camera shots, forcing Android to force quit apps running in the background.  Have a look at the Android log via “adb logcat” or “ddms” to see if this is happening.

You can also try requesting the Android OS to give your app a larger heap (ie: more memory) by adding the following setting to your “build.settings” file.  Note that typically only Android 4.0 or higher devices respect this setting.

settings = { android = { largeHeap = true, } }

Thanx for your help!

I tried the “largeHeap” setting, but it did not change the behaviour. This is probably due to the fact my device is only running Android 2.3.5.

I searched the logcat output intensely. I could not find any hints that were obviously related to the issue besides the line I already noted in my first post.

But I must admit that it is very difficult to analyze and understand the whole logcat output without deep knowledge of the Android OS.

Because I could reproduce the problem with the camera sample app, I think there must be other people that encountered this issue too. If it only happens on very specific and rare device / OS configurations, I can live with it.

But if it happens on many devices (especially if they are only a few years old and use the highest camera resolution, which is a common usecase in my opinion), this is quite a dealbreaker for apps using the camera!

I have older devices than yours, such as a 1st generation Droid and an HTC Evo 4G which are both running Android 2.2, and they don’t have this issue.  The camera sample app works fine on them.

Does rebooting your phone help any?  (I’m hoping it will close all other apps and free up memory.)

Or perhaps your device is out of storage space?

One more thing.  You said you sometimes see a Corona “Trial” popup when displaying your app.  The newest release version of Corona, build #1076", should no longer displays a trial popup.  So, have you tried upgrading to the newest release version?

Storage space is probably not the problem. My internal space is above 400 MB, external over 5 Gigs.

I furthermore downloaded the most recent version of the Corona SDK.

This did not fix the problem, but introduced a new issue with the ScrollView. The “takeFocus(event)” function of the ScrollView seems to be “nil” now. The same code was working before. Have there been any changes?

But rebooting the device really did help! Afterwards I could not provoke the error anymore.

But I guess the end-user will not be happy with this “workaround”. Are there other possibilities, or shall we cancel this issue as too rare to be dealt with?

If rebooting solved the problem, then it sounds like you had too many apps running in the background eating up system memory.  What likely happened was that displaying the camera activity window forced other apps that were running in the background to be shutdown, and unfortunately, your app was one of them.  I don’t think we can do anything about that.  Android does not give us permission to force quit other apps running in the background because that would be a security issue.  Also, Corona frees up as much memory as possible by unloading all textures/images before suspending itself to prevent Android from force quitting the app.  That’s as much as we can do.  I will say that this is the first time I’ve heard of this happening, so I hope that makes this a very rare case.

The only other thing I can offer is instead of displaying the device’s default camera app for taking pictures, you can use Corona’s built-in camera activity instead.  You can do this by *removing* the “WRITE_EXTERNAL_STORAGE” permission, but you still have to set the “CAMERA” permission.  Corona’s camera activity is very basic though because it was meant to be used as a fallback mechanism in case a camera app could not be found on the device.

Anyways, I hope this helps.

I have a similar issue on my Samsung S2, and my clients S3.  I have added the code “largeHeap = true” and I will let you know if this helps…

It is very important for me to understand the cause for this problem. Your explanation seems quite feasible.

I have tried Corona’s built-in camera app, and first tests went very well. There were no more unexpected restarts. The user can take pictures very easily. Does it work on iOS the same?

Disadvantages are that users cannote “retake” a picture (not a big deal for my app), and the camera preview seemed a bit distorted (but the final pictures were not).

Even an advantage for my app is the fact that pictures taken with the Corona camera-app are not included in Android’s gallery (which was kind of annoying with the native camera-app).

Thanx for this info, I did not know that there even was a built-in camera app. I think a description to this fact would be an important addition for the API-documentation of “media.show”.

Come to think of it, I think setting largeHeap to true might make it worse, because if the device is running low on memory, then the OS is more likely to quit apps that use the most memory on the device first.  Although I’m shocked that the operating system would decide to take out apps that belong to the same “task” as the app that launched it.

I did a quick Internet search about this issue and discovered that this is unfortunately a common issue on Android…

http://stackoverflow.com/questions/7473061/startactivityforresult-but-not-on-my-apps-activity-stack

http://stackoverflow.com/questions/10910227/startactivityforresult-in-low-memory-situations?rq=1

Do you happen to have a lot of other things loaded in memory such as audio?  If so, then perhaps you should dispose of your audio data when your app is being suspended so that Android is less likely to take out your app during these memory starved situations.

http://docs.coronalabs.com/api/library/audio/dispose.html

You may also want to call Lua’s collectgarbage() function to ensure all unreferenced Lua objects get collected as well.

Other than that, I’m not sure what else can be done since this is an out-of-memory issue.  The reason displaying Corona’s internal Corona camera activity works around this problem is because it belongs to your app.  So, that’s the only guaranteed way around this, but unfortunately I don’t think we can detect these out-of-memory situations until it’s too late.  Hmm…