Reputation: 451
About the app:
I'm trying to make a simple flashlight app with just a togglebutton in its UI. Since I want the flashlight to be ON even when the app is minimized, I have opened and released the camera in the onClick() event.
The Problem:
My app works when first opened. Tried switching the flashlight ON and OFF multiple times and it works. However, when I restart the app, it closes unexpectedly.
A big problem is when I start the app for the first time, press back button without using the app and restart the app, it still closes unexpectedly. And if i start my app for the first time, press back without using the app and start my camera, "Camera not available" toast appears.(My code has just two variables declared and absolutely no code added in the onCreate() section, so I don't know how/why my app is holding camera resources).
Another problem is the emulator shows no error. (╯°□°)╯︵ ┻━┻
PS: android.permission.CAMERA Permission has been set.
Code:
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
Camera camera=null;
Parameters p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void flash(View view) {
// Is the toggle button on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable torch
camera = Camera.open();
p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
} else {
// Disable torch
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release();
}
}
}
App doesn't crash on the emulator on starting the app, pressing back without using and restarting the app (Unlike on my phone). But Still posting the LogCat.
On starting the app for the first time:
10-15 16:05:50.229: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.529: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.529: I/Choreographer(11244): Skipped 49 frames! The application may be doing too much work on its main thread.
10-15 16:05:51.539: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.749: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.799: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:51.999: D/dalvikvm(11244): GC_CONCURRENT freed 128K, 9% free 2643K/2888K, paused 30ms+98ms, total 700ms
10-15 16:05:51.999: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.019: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.031: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.139: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.139: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.189: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.219: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.249: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.249: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.320: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:05:52.400: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
On Pressing Back:
10-15 16:08:05.559: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.579: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.609: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.629: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.720: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.908: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.919: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:05.929: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:06.169: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:06.189: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:07.149: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:07.359: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.009: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.009: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.029: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.219: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:08.219: I/Choreographer(11244): Skipped 118 frames! The application may be doing too much work on its main thread.
On Restarting:
10-15 16:08:47.629: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:48.909: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:48.909: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.108: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.169: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.379: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.389: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.389: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.450: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.489: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.500: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.500: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.509: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.519: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.619: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
10-15 16:08:49.730: W/Trace(11244): Unexpected value from nativeGetEnabledTags: 0
Additional Info: I have checked these two questions posted on this site and found one unrelated and other too complex. (url1 url2)
**Modified the code to make it work, but the cause of previous problem is still unknown to me. **
New Code:
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
Camera camera=null;
Parameters p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = Camera.open();
p = camera.getParameters();
}
public void flash(View view) {
// Is the toggle button on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable torch
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
} else {
// Disable torch
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
}
}
protected void onResume()
{
super.onResume();
try
{
camera.reconnect();
}
catch(Exception e)
{}
}
protected void onPause()
{
super.onPause();
try
{
camera.release();
}
catch(Exception e)
{}
}
protected void onStop()
{
super.onStop();
try
{
camera.release();
}
catch(Exception e)
{}
}
}
Upvotes: 1
Views: 1220
Reputation: 44571
I believe most of your problems are due to not closing the Camera
resources properly when leaving the app. If you do it while the light is on then you haven't released the resources
.
According to the docs the last step in using the Camera
object
says this:
Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).
So you should probably override onPause()
and add this to make sure the Camera
resources are available the next time you run the app or try to use the camera in a different app.
@Override
public void onPause()
{
camera.release();
super.onPause();
}
Also, you should post your logcat when your app crashes so that we can see exactly what and where the exception is.
Upvotes: 2