0

I am trying to make the camera flashlight blink. I already have written a code for switching the flashlight on and off. I am trying to create a method which could blink the flashlight on the click of a button.

How can i achieve this.

public void flash_effect() throws InterruptedException
{
    camera = Camera.open();     
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);


    Thread a = new Thread()
    {
        public void run()
        {
            for(int i =0; i < 10; i++)
            {
                camera.setParameters(params);
                camera.startPreview();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                camera.stopPreview();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    };
    a.start();
}

This code is not working.What am i doing wrong. Waiting for help.

EDITED

Manifest

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

        <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

LOGCAT

06-16 14:03:40.579: E/AndroidRuntime(20302): FATAL EXCEPTION: main 06-16 14:03:40.579: E/AndroidRuntime(20302): java.lang.IllegalStateException: Could not execute method of the activity 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.view.View$1.onClick(View.java:3626) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.view.View.performClick(View.java:4231) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.view.View$PerformClick.run(View.java:17537) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.os.Handler.handleCallback(Handler.java:725) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.os.Handler.dispatchMessage(Handler.java:92) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.os.Looper.loop(Looper.java:158) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.app.ActivityThread.main(ActivityThread.java:5751) 06-16 14:03:40.579: E/AndroidRuntime(20302): at java.lang.reflect.Method.invokeNative(Native Method) 06-16 14:03:40.579: E/AndroidRuntime(20302): at java.lang.reflect.Method.invoke(Method.java:511) 06-16 14:03:40.579: E/AndroidRuntime(20302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083) 06-16 14:03:40.579: E/AndroidRuntime(20302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 06-16 14:03:40.579: E/AndroidRuntime(20302): at dalvik.system.NativeStart.main(Native Method) 06-16 14:03:40.579: E/AndroidRuntime(20302): Caused by: java.lang.reflect.InvocationTargetException 06-16 14:03:40.579: E/AndroidRuntime(20302): at java.lang.reflect.Method.invokeNative(Native Method) 06-16 14:03:40.579: E/AndroidRuntime(20302): at java.lang.reflect.Method.invoke(Method.java:511) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.view.View$1.onClick(View.java:3621) 06-16 14:03:40.579: E/AndroidRuntime(20302): ... 11 more 06-16 14:03:40.579: E/AndroidRuntime(20302): Caused by: java.lang.RuntimeException: Fail to connect to camera service 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.hardware.Camera.native_setup(Native Method) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.hardware.Camera.(Camera.java:362) 06-16 14:03:40.579: E/AndroidRuntime(20302): at android.hardware.Camera.open(Camera.java:336) 06-16 14:03:40.579: E/AndroidRuntime(20302): at com.example.testlight.MainActivity.flash_effect(MainActivity.java:185)

1 Answers1

0

I've been working on your problem, and my solution can blink the flashlight. I used your same logic, except I used Handler instead of Thread to delay the blink.

public void flash_effect(View view)
{
    long delay = 50;
    camera = Camera.open();
    params = camera.getParameters();
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

    camera.setParameters(params);
    camera.startPreview();

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            camera.release();

        }
    }, delay);

}

You have to initialise the handler before using it. Do it in the onCreate() method.

 Handler handler = new Handler();

This method is an onClick() of a button. I used a button to start a blink

 public void flash_effect(View view)
capt.swag
  • 10,335
  • 2
  • 41
  • 41
  • The above code worked perfectly on mine. Could you post the logcat. – capt.swag Jun 16 '15 at 08:37
  • You haven't set the onClick of your button which starts the blinking to flash_effect – capt.swag Jun 16 '15 at 09:19
  • ok, then remove all the codes inside public void flash_effect(View view) and just try Log.d("view", "logging succesful");. I seriously believe that it's a problem with button handling. – capt.swag Jun 16 '15 at 14:00
  • I have made a temporary github repostory of my project which has uses the same above code, and a button to invoke flashlight blink https://github.com/4k3R/Flashlight – capt.swag Jun 16 '15 at 15:27
  • hey, does this blink continuously with the press of a button? – Emmilie Frank Jun 16 '15 at 15:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80688/discussion-between-emmilie-frank-and-4k3r). – Emmilie Frank Jun 16 '15 at 15:34