I am using a Camera, and I call the open method on it in onCreate in an activity. When the activity is destroyed, I release the Camera. However, a fragment in the activity may be using the Camera (for example, making the flash blink repeatedly), so when the orientation changes from portrait to landscape, for example, the Camera is released (because the activity is destroyed) and the reference is set to null, causing a NPE to be thrown from the fragment. How do I keep the reference to the Camera during orientation change (so that the flash keeps blinking) and release the Camera when the activity is destroyed (for example, when the user clicks the Back button)?
public static Camera camera = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (camera == null) {
camera = Camera.open();
if (camera == null) {
Log.e(LOG_TAG, "Unable to get back-facing camera.");
finish();
}
}
// ..
}
#Override
protected void onDestroy() {
if (camera != null) {
camera.release();
camera = null;
}
super.onDestroy();
}
Try this android:configChanges="keyboardHidden|orientation"
For api 13 or higher android:configChanges="keyboardHidden|orientation|screenSize"
As of api 13 screen size also changes when a device switches between portrait and landscape mode
So this way your activity will not be destroyed during orientation change
Related
I am using the camera flash for a Morse code application. I create a new camera object when the class is created. The user has a button which is used to reset and also release the camera when required (If they want to stop the light Morse sequence prematurely).
The problem is that when they hit the reset button because the activity is not created or loaded again the camera never get reinitialized - this is a problem because the method of the class that it is calling is used by another class and releases the camera when it is has completed it's function. I am not sure how to structure the code in a way that allows me to do this.
I am wondering if anyone has any advice/suggestions in how to achieve this?
//Camera object being declared
Light light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__morse);
//Camera object being initialized
light = new Light
}
//Reset user pressed a button
public void reset(View view)
{
light.release();
}
//Releasing the camera
public void release() {
if(camera != null)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
Don't initialize the camera in onCreate. Do it in onStart, and release it in onStop.
I have a live wallpaper that just draws triangles, very simple, and runs smoothly all time. After testing found that when I open the camera to take pictures the phone freezes for about 10 seconds, then the camera opens and the wallpaper crashes and the message "Unfortunately, Live Wallpaper has stopped." appears. Also found that when I open any barcode scanner app the problem raises again because the scanner uses the camera too. Seems the problem raises when the camera app runs. Any ideas what's causing it?
hi #Bullet Camera is just open ones, if it open through any other apps then, you can can not access your camera, Solution is that, where you use camera please release that after no used.
main problem is that, in your app (app in that you are using camera) , so , you release camera after used.
like following :
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
when you need
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
or
public void stopCamera() {
if (cameraDevice != null) {
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
System.out.println("in to the stop video");
}
}
I am making an android flashlight app. I it has two activities, a main activity and a settings activity where the camera led can be toggled on and off. It also has another class where all the camera changes are handled like opening it, releasing it, and turning the light on and off.
I kept getting errors when turning on the led based on shared preference settings because I was not opening and releasing the camera in the correct activity lifecycle stages. I fixed the issues by releasing the camera when onPause is called in either activity and turning the led on or off (based on shared preference settings) when onResume is called in either activity.
The problem I am having now is that if the led is on, it turns off briefly when switching from one activity to the other because I have to release the camera and then open again in the new activity. Can anyone help me figure out a way to eliminate this problem? Where should I open and release the camera? I have tried releasing it in on destroy but the led stays on when the app is minimized to the background, which is undesirable. Thanks for any suggestions.
In both main and settings activity I have:
#Override
protected void onResume() {
super.onResume();
if (sp.getBoolean("LED_TOGGLE_CB", false) == true) {
flash.turnFlashOn();
}
}
#Override
protected void onPause() {
super.onPause();
flash.killCamera();
}
In the Flash class I have:
void getCamera() {
if(camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch(RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
void turnFlashOn() {
getCamera();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
}
public void killCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
i have develped a camera app which uses camera API from this link
its working perfectly it shows preview takes picture but when i finish the app it never releases camera ...i can't use camera solely after installing this app ...even the app get crashed when i restart it.
here's how i am releasing the camera-->
#Override
protected void onPause() {
super.onPause();
if (camera != null) {
// preview.setCamera(null);
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
onResume i put this code also but didn't work..s
#Override
protected void onResume() {
super.onResume();
camera = Camera.open();
preview.setCamera(camera);
}
I have same problems on HTC One X (4.1.2) and Nexus 4(4.3), but on Galaxy Nexus (4.3) same release method works great.
I temporary solved this issue with using camera.unlock() instead of camera.release() and keeping static reference to Camera object.
I have a problem. After initializing the camera for a preview and bringing another app into focus, then back to my app: the preview shows up black. If I continue to take a picture, it takes a picture of where I point the camera normally.
Am I doing something wrong on the OnResume() override? Relative code is below:
public void ReleaseCamera()
{
if (myCamera != null)
{
myCamera.Release();
myCamera = null;
}
}
protected override void OnPause()
{
base.OnPause();
if (myButtonState == ButtonState.CameraActive)
ReleaseCamera();
}
protected override void OnResume()
{
base.OnResume();
if (myButtonState == ButtonState.CameraActive)
InitializeCamera();
}
private void InitializeCamera()
{
SurfaceView mySurfaceView = FindViewById<SurfaceView>(Resource.Id.surfaceView1);
myCamera = Android.Hardware.Camera.Open(cameraNumber);
Android.Hardware.Camera.Parameters p = myCamera.GetParameters();
myCamera.SetDisplayOrientation(90); // Portrait
myCamera.SetPreviewDisplay(mySurfaceView.Holder);
myCamera.StartPreview();
}
Thank you for your help. :)
onResume() gets called too early. You don't have the surface holder ready at this stage. You can try to introduce onPostResume() handler in your Activity, and/or handle the SurfaceHolder.Callback.surfaceChanged() event.