Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,782,422 Members 43,412 Now Online
XDA Developers Android and Mobile Development Forum

Another Camera Fix (It's really a workaround) to try

Tip us?
 
drklaw
Old
(Last edited by drklaw; 29th June 2014 at 02:29 PM.)
#1  
drklaw's Avatar
Member - OP
Thanks Meter 70
Posts: 68
Join Date: Mar 2013
Short version:

1. Download the apk posted below and install it.
2. Set the app to run on boot using the method of your choice (I use Tasker).

Updated app link:
http://forum.xda-developers.com/show...php?p=52696903
The Following 19 Users Say Thank You to drklaw For This Useful Post: [ Click to Expand ]
 
drklaw
Old
(Last edited by drklaw; 18th May 2014 at 03:14 AM.)
#2  
drklaw's Avatar
Member - OP
Thanks Meter 70
Posts: 68
Join Date: Mar 2013
Long version:

Hello there,

This is basically a call for a bit of dev help for a potential workaround I think I may have stumbled upon for fixing the annoying hanging camera issue we seem to have on a bunch of the CM/AOSP based roms. The symptoms of the issue are:

1. Reboot phone
2. Open any camera app - there's a frozen frame for a moment then it either
a. works normally
b. freezes and camera apps then "cannot connect to camera" until you reboot and try again.

The issue I was seeing on my phone (running the awesome SlimKat ROM) was that sometimes it would end up at 2a, and sometimes at 2b. I could not see any pattern, rhyme, or reason for it. Until...

I opened the ROM's camera app (set to store on my SD card) with my SD card removed. This caused the camera app to crash before showing any camera preview. I then noticed that once I put the SD card back in the camera app would open and work perfectly and immediately. No freezing at all. I have replicated about 20 times successfully, with no instances of 2b.

That made me think. I've done some Android head tracking coding in the past and worked with camera previews and I know that to get a camera preview running, you first open the camera object then start the preview. Since normally, sometimes the situation would be 2a and sometimes 2b, that made me think that there might be a race condition happening between the "camera open" step and the "show preview" step.

So I made a simple app to open the camera, wait 5 seconds, and close, without showing any preview. I set this app to run on boot-up using Tasker. (This is meant to replace the manual SD card trick I was using, outlined above.)

I'm sharing this with the hope that the ROM developers, who are far more skilled than I, might use this information. I attached my apk (a better one is posted later in this thread with a link to that post in the next post), and I'll give you the code (again, updated later). I hope you can use it to confirm or reject my findings or even do a better job integrating it into your ROM then I did with my boot-up hack.

Thanks!

Code:
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;

public class MainActivity extends Activity implements SurfaceTextureListener{

    private Camera mCamera;
    private TextureView mTextureView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);

        setContentView(mTextureView);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();
             
        try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
        mCamera.release();        
                        
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, the Camera does all the work for us
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Update your view here!
    }
}
The Following 15 Users Say Thank You to drklaw For This Useful Post: [ Click to Expand ]
 
drklaw
Old
(Last edited by drklaw; 18th May 2014 at 03:11 AM.)
#3  
drklaw's Avatar
Member - OP
Thanks Meter 70
Posts: 68
Join Date: Mar 2013
This is the apk mentioned above:

http://forum.xda-developers.com/show...php?p=52696903
The Following 8 Users Say Thank You to drklaw For This Useful Post: [ Click to Expand ]
 
peddarson
Old
#4  
Senior Member
Thanks Meter 48
Posts: 268
Join Date: Aug 2012
Quote:
Originally Posted by drklaw View Post
Long version:

Hello there,

This is basically a call for a bit of dev help for a potential workaround I think I may have stumbled upon for fixing the annoying hanging camera issue we seem to have on a bunch of the CM/AOSP based roms. The symptoms of the issue are:

1. Reboot phone
2. Open any camera app - there's a frozen frame for a moment then it either
a. works normally
b. freezes and camera apps then "cannot connect to camera" until you reboot and try again.

The issue I was seeing on my phone (running the awesome SlimKat ROM) was that sometimes it would end up at 2a, and sometimes at 2b. I could not see any pattern, rhyme, or reason for it. Until...

I opened the ROM's camera app (set to store on my SD card) with my SD card removed. This caused the camera app to crash before showing any camera preview. I then noticed that once I put the SD card back in the camera app would open and work perfectly and immediately. No freezing at all. I have replicated about 20 times successfully, with no instances of 2b.

That made me think. I've done some Android head tracking coding in the past and worked with camera previews and I know that to get a camera preview running, you first open the camera object then start the preview. Since normally, sometimes the situation would be 2a and sometimes 2b, that made me think that there might be a race condition happening between the "camera open" step and the "show preview" step.

So I made a simple app to open the camera, wait 5 seconds, and close, without showing any preview. I set this app to run on boot-up using Tasker. (This is meant to replace the manual SD card trick I was using, outlined above.) So far, I've run it about 5 times and the camera has been starting right up for me.

I'm sharing this with the hope that the ROM developers, who are far more skilled than I, might use this information. I attached my apk, and I'll give you the code. I hope you can use it to confirm or reject my findings or even do a better job integrating it into your ROM then I did with my boot-up hack.

Thanks!

Code:
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;

public class MainActivity extends Activity implements SurfaceTextureListener{

    private Camera mCamera;
    private TextureView mTextureView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);

        setContentView(mTextureView);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();
             
        try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
        mCamera.release();        
                        
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, the Camera does all the work for us
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Update your view here!
    }
}
Sounds good. A nice and simple solution actually.
But one question: why does your workaround waits for five seconds? Is it possible to decrease the time?
 
drklaw
Old
#5  
drklaw's Avatar
Member - OP
Thanks Meter 70
Posts: 68
Join Date: Mar 2013
Quote:
Originally Posted by peddarson View Post
Sounds good. A nice and simple solution actually.
But one question: why does your workaround waits for five seconds? Is it possible to decrease the time?
Probably. I just arbitrarily chose that based on my estimate of the time the camera would freeze when starting.
 
abbychauhan
Old
#6  
abbychauhan's Avatar
Senior Member
Thanks Meter 683
Posts: 1,036
Join Date: Dec 2013
Location: JAIPUR
I got ur app and Tasker app but dont knw how to use Tasker to set ur app to run on boot..
Can someone help me with Tasker??

Sent from my Micromax A110Q using Tapatalk
 
drklaw
Old
(Last edited by drklaw; 11th May 2014 at 05:38 PM.)
#7  
drklaw's Avatar
Member - OP
Thanks Meter 70
Posts: 68
Join Date: Mar 2013
Make a task to open the app, then make a profile to run that task on device boot.
The Following User Says Thank You to drklaw For This Useful Post: [ Click to Expand ]
 
shamps
Old
#8  
shamps's Avatar
Senior Member
Thanks Meter 109
Posts: 760
Join Date: Jan 2011
Works fine with PAC rom. Thanks!!

Cheers!

Sent from my Xperia Z1
 
Juzman
Old
#9  
Senior Member
Thanks Meter 194
Posts: 984
Join Date: May 2011
Location: Townsville

 
DONATE TO ME
Great find. Would there be a way to implement an option to load a certain camera app too in the meantime? Have an "options" for the APK to choose say Google Camera, Camera2, or other camera app. Then use the main APK you made, to launch the non-preview then load the actual camera app?

I don't use Tasker, and figured this way, I could hide my Camera app from the app drawer, and just use this as a standalone.

1. Tap the "Fixed" apk
2. Launches non-preview and exits
3. Launches chosen camera app.

Phone: Nexus 5 | Sony Xperia Z1 | Sony Xperia Z | Galaxy S2
Rom: ParanoidAndroid
Radio: KTU84Q
Kernel: franco.Kernel (My Voltages) and my own tweaks
Mods: GPU Drivers | LG G3 Bionic | media_profile.xml


My Nexus 5, Xperia Z1, Xperia Z, STweaks (S2) and TricksterMOD (N7) settings.


If I've helped you, please press thanks!
 
B1nny
Old
#10  
Forum Moderator
Thanks Meter 586
Posts: 1,042
Join Date: May 2008
Location: The Hague

 
DONATE TO ME
Quote:
Originally Posted by Juzman View Post
Great find. Would there be a way to implement an option to load a certain camera app too in the meantime? Have an "options" for the APK to choose say Google Camera, Camera2, or other camera app. Then use the main APK you made, to launch the non-preview then load the actual camera app?

I don't use Tasker, and figured this way, I could hide my Camera app from the app drawer, and just use this as a standalone.

1. Tap the "Fixed" apk
2. Launches non-preview and exits
3. Launches chosen camera app.
This is possible but the application only needs to be ran once. If it can be ran at boot in the background for example you won't notice a thing plus you won't have to wait 5 seconds before you open the actual camera application. A thing you might be concerned about is a performance impact at boot, I think this will be very minimal though since the Z1 is a very powerful device.

It's a very interesting workaround for sure and until a real fix has been found it would be nice to have a better way of implementing this. For now, it seems to work great! So good work OP!
"However, if you put any trust in Benchmark Scores you could use them
to prove that dancing naked for 5 minutes in your garden affects device performance" - Chainfire

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes