1

I am trying to make a flash light app to run on a Galaxy Nexus. I am aware that you need to implement SurfaceHolder.Callback. But when I do this, the app is compiling and running with no errors. But then LED is not turning on at all, and the app crashes after pressing the toggle button 3 times.

public class MainActivity extends Activity implements SurfaceHolder.Callback {
Camera cam;
public static SurfaceHolder mHolder;
public static SurfaceView preview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preview = (SurfaceView) findViewById(R.id.PREVIEW);
    mHolder = preview.getHolder();
    cam = Camera.open();
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
    final TextView text = (TextView) findViewById(R.id.textView1);
    text.setText("Flash Light");

    toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub


            if(isChecked) {

                text.setText("Flash Light is ON");
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();

            } else {

                text.setText("Flash Light is OFF");
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                cam.release();

            }

        }

    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mHolder = holder;
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    cam.stopPreview();
    mHolder = null;

}
}

EDIT : Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flashlight"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT"/> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-feature android:name="android.hardware.camera.Parameters" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:debuggable="true">
        <activity
            android:name="com.example.flashlight.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

ANSWERED:

I figured out that in the activity_main.xml I needed to added under my SurfaceView the LAST LINE:

activity_main.xml

<SurfaceView
android:id="@+id/PREVIEW"
android:layout_width="1dip"
android:layout_height="1dip"
android:visibility="visible" />
flash
  • 265
  • 4
  • 15
  • put your manifest too. – Hosein Feb 09 '13 at 06:30
  • im sorry, i didnt know how to separate the manifest from the main activity. but i added it nonetheless. – flash Feb 09 '13 at 06:41
  • look at [HERE](http://stackoverflow.com/questions/6068803/how-turn-on-camera-flash-light-programmatically-in-android) , i think it can help you. – Hosein Feb 09 '13 at 06:48
  • yes I have seen these before. I just thought that checking for the availability of a camera was extra. But is it FULLY NEEDED to turn the flash on? and thank you for the manifest edit – flash Feb 09 '13 at 06:50
  • i didn't work in this field. but why you check changing , not state of toggle button? i mean just check it if it's true, do something, if it's false do something else. i think it's better to just check it on changing. – Hosein Feb 09 '13 at 07:00
  • I just checked is a camera was available on the device and the application ran but same problems occured: crashed on 3rd time toggling switch, and LED doesnt turn on. – flash Feb 09 '13 at 07:00
  • i think your problem is in permissions. look at this part of link that i comment. "For turning on/off flashlight :" your permission is not complete. – Hosein Feb 09 '13 at 07:04
  • Thank you for your help. But that didnt work. it says "No resource found at given name "label" for "@string..."". I have no idea what i am doing wrong – flash Feb 09 '13 at 07:17
  • ok. all things i can say is that, read that answer again. your answer is there. – Hosein Feb 09 '13 at 07:30
  • what is the exception being thrown? – CChi Feb 09 '13 at 07:32
  • Thanks to CChi, I figured out that the reason my program was crashing because the exception being thrown was: "02-09 12:39:22.102: E/AndroidRuntime(6573): java.lang.RuntimeException: Method called after release()" And this was happening because of the cam.release() method. So when I remove that program does not crash anymore. But the Flash is still not turning on. – flash Feb 10 '13 at 04:21

1 Answers1

0

Check whether flashModes are available or not. And also make you Camera mCamera static otherwise when orientation change it loses camera instansce.

public static void FlashOn(Camera mCamera)
    {
        //Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        //Check Whether device supports AutoFlash, If you YES then set AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }
        else if(flashModes.contains(Parameters.FLASH_MODE_ON))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

    public static void FlashOff(Camera mCamera)
    {
        //Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        //Check Whether device supports AutoFlash, If you YES then set AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300