Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application hangs / freeze when using camera.open() - Restart of device needed

My application is quite basic and simple, I just need to open the camera (Not even displaying images, I commented the code)

So, basically, I have something like:

@Override
public void onResume() {
    super.onResume();
    if (camera != null) {
        //Just to be sure
        camera.stopPreview();
        camera.setPreviewCallback(null);
        camera.release();
        camera = null;
    }

    Log.e("DebugCam", "Before");
    camera = Camera.open();
    Log.e("DebugCam", "After");
}

@Override
protected void onPause() {
    super.onPause();
    if (camera != null) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }
}

But my logcat is just displaying the first message:

Before

The app is just hanging, and I have to restart the device to make it work again!!!

I really don't understand where is the problem or how I could at least avoid the freeze that is really annoying.

I really don't get how I could debug this...

Edit: I have seen this on Android bug tracking, sounds like I need a workaround as the patch was released in June) https://code.google.com/p/android/issues/detail?id=1578

like image 936
Waza_Be Avatar asked Jul 15 '13 13:07

Waza_Be


2 Answers

I wanted to summarize all the information on the issue that I managed to find.

I've done some research and I found an interesting and probably not so tightly related thread about a similar problem in the DroidForums:

There they found out, that the SD card was causing the freeze (I'm not sure if that's the case now, but it will be good if someone can try it) and here's the quote from there:

My camera and camcorder were freezing up like many other people's and i was frustrated..in my hands i have my "Like-New" phone hoping that would help...no such luck...figured out what it was...THE SDHC CARD! simple format of the card and issues went away..to test this yourself go to your main settings and unmount the sd card and then restart the camera app and see if it doesnt freeze...if it doesnt like mine didnt then you have your answer...not exactly sure what on my SDHC card caused this...still under investigation, but definitely something there...i will update when i find out exactly what for everyone else

Hope this helps and thanks to all for the many tips I have used from this forum...

UPDATE!!!

I just had the same issue happen again with my new Gingerbread phone and I tried something new...just move some photos and vidoes off of the phone and back them up to PC...worked like a charm...no need to format entire card, just give the camera/ camcorder app some space..as if 5.5GB wasn't enough, but hey who knew...now videos only take 1/2 second to save and camera is working fine again...just thought i would let all of you know..

In general, it seems that the memory card is causing the problem sometimes (or in most of the cases), so if you can format the one you're using or replace it with a new one you can see if that's the issue.

Further, on the official bug, which was logged here (as you mentioned): Apparently the test case is the same as the one you have presented here, as the applications which use Camera.open() are experiencing the problem on the exact same place.

Quote from there:

Apparently, Camera.open() itself hangs without causing any exception, such that there is nothing that I can trap and act upon. I can then also not use the built-in camera app, as it gives just a black preview screen and a bit later a message about being unresponsive when I try to interact with it. Since in the above code snippet was already null (at program startup), there is just nothing that I can release: no camera.release().

It's good that this was fixed in the latest release and several days ago there was a comment (the last one, which is from Jul 15, 2013):

Thanks for the bugfixe, but is there a workaround for device older than 22 June 2013, when the patch was released (wich means after 4.2.2 released in february 2013...)

Hopefully we'll get updated on this one, so let's wish we're going to hear news there for older APIs.

Further I found the thread here (again, mentioning an SD card problem; not much info there, but anyway):

Quote from there:

And the solution found: I deleted the thumbnails on the image folder of my SD card and restarted the phone. It seems that the little thumbnail window which indicates the last shot was responsible for the app crash, for some reason the thumbs was unable to load and the application was crashing.

Then, there was this thread on the xda-Developers named "4.2 Android camera APK FIX!" (don't get excited, this is for ROOTED devices only :-( ):

Quick instructions taken from the thread:

Camera apk: http://db.tt/xmpgkZIA

Add this apk to system/app after installing camera apk first only for manual. http://db.tt/OZF6D8fL

This are the lib files place them on system/lib fix permission then reboot. http://db.tt/73lcCrcl http://db.tt/wQ7bfR8N

For those with device not supported, change the following lines in build.prop and reboot:

ro.product.model=Galaxy Nexus ro.product.brand=google

Next (just to mention it to the broad public), I found this in the AndroidCentral, just for the sake of it, it's again a "fix" for rooted devices.

On the website sebertech, I found this thread regarding a camera problem with Galaxy S3 and it's TouchWix mainly - LINK:

For Galaxy S3 units that are plagued with performance problems, rooting is the first step to resolving those issues. But you have to take into consideration the risks involved, although there are already safe ways to root the phone. But before you actually decide to do that, I have some things I want you to try.

  1. Recall apps that you’ve recently installed, try to disable them and see if there are improvements in the performance of your phone. Some really heavy apps may have even caused or contributed to the problem.
  2. Try clearing the memory by going to the Home screen and holding on the Home button. A list of running or recently opened apps will show; you just have to swipe left or right to close them. I’m pretty sure you already knew the effects of having so many apps running in the background.

Limit Galaxy S3 Processes

  1. From the Home screen, press the Menu button and choose Settings.
  2. Scroll down to and tap on Developer Options.
  3. Find “Limit background processes” option and tap on it.
  4. Limit the Galaxy S3 to run about 4 processes at once, or lower.

Clear the data for TouchWiz

  1. From the Home screen, tap on Menu button.
  2. Choose System Settings then Application Manager
  3. Scroll down to find TW and tap Clear Data.

If nothing else works, perhaps it’s time to root your device and use lighter and faster custom ROMs and get rid of the TouchWiz UI.

Finally, to finish this long answer I would like to say that, even though I found very few fixes/workarounds for this, I really hope, that the Official Bug Fix (linked again, in order to have this thing quoted once more) is going to be broadly available and not only for 4.x.x versions of Android, it's a pity that we don't have it for older versions (what's the deal with a bug fix for older APIs anyway?).

like image 79
g00dy Avatar answered Nov 09 '22 14:11

g00dy


try this :

  @Override
 public void onResume() {
super.onResume();
if (camera != null) {
    //Just to be sure
    camera.stopPreview();
    camera.setPreviewCallback(null);
    camera.release();
    camera = null;
}

Log.e("DebugCam", "Before");
try {
    camera = Camera.open();// this is no safe way to open the camera
}catch(Exception e)
{
  e.printStackTrace();//get the exception on Logcat
}
Log.e("DebugCam", "After");
}

Direct from DOCS

/** A safe way to get an instance of the Camera object. */

public static Camera getCameraInstance(){
Camera c = null;
try {
    c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
    // Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}

Look this: http://developer.android.com/guide/topics/media/camera.html#custom-camera

like image 36
Tobiel Avatar answered Nov 09 '22 14:11

Tobiel