0

I have looked into other camera flashlight related problems in stackoverflow, but couldn't find an answer that solves my issue. The flashlight flashes for 6-8 times and then the app crashes. Here I have a blink() method that calls cameraon() and cameraoff() in a loop. Could you please let me know where I went wrong? My code can be found here: http://pastebin.com/3LRMwd1J The logcat output can be see here: http://pastebin.com/2GTpn8Ux

I have tried using surface textures, tried to include Thread.sleep() in betweencameraon() and cameraoff() but so far no luck.

I use Android 4.4 with latest sdk and jdk versions. I use nexus 5 for testing.

Thanks in advance!

user3891554
  • 81
  • 2
  • 9
  • Please post your code in your question as well, some are unable to get to pastebin due to proxies – JBires Jul 30 '14 at 13:52

3 Answers3

1

just take Thread in Blink() method and put your code with if else

boolean tourchon=false;

Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
            for (int i = 0; i < 10; i++) {

             if(tourchon){
               cameraoff();
               tourchon=false;
             }else{
                cameraon();
               tourchon=true;
             }
         try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
         }
         }
        });
        thread.start();

Try this code...

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
  • Hey,Thanks for your help. However, this code did not work. It shows this error: http://pastebin.com/e44wbQr1. Now, it crashes immediately without any blinks. – user3891554 Jul 31 '14 at 07:40
1

Separate the obtaining of camera object from your cameraOn() method, as i understand this method should be turning the flash on, calling open() that many times in that short time may be causing your problems as this documentation suggests.

Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.

So have your on and off methods just change the flash mode, because you already have the camera instance.

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • Hi, Thank you for the help. I am not sure but is this what you meant http://pastebin.com/njhPZ9Jb ? It just blinks for one time for nearly 2 secs. Doesn't give any error this time. How do I make it blink continuously for a longer time? – user3891554 Jul 31 '14 at 10:13
  • I was trying the solution that Jay has proposed. I deleted the thread and tried, it just blinks for one time. Gives the warning dequeueBuffer: BufferQueue has been abandoned! – user3891554 Jul 31 '14 at 10:33
  • You can refer to that topic http://stackoverflow.com/questions/21064391/how-to-create-fast-or-slow-camera-flash-blink-signals-in-android – elmorabea Jul 31 '14 at 10:39
  • Well I have tried that yesterday. It doesn't blink nor crashes. :-) – user3891554 Jul 31 '14 at 11:00
0

See if my code helps any: https://github.com/wolfhorse/SimpleFlashlight

It doesn't blink but it may be a good reference for you.

EDIT: I modified the onClick() event of the toggle button in the SimpleFlashlight app referenced above by adding the code below and it blinks/flashes fine on my Samsung Galaxy S5 without error.

Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        boolean flashOn = false;
                        for (int i = 0; i < 20; i++) {
                            flashOn = !flashOn;
                            toggleFlash(flashOn);
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                thread.start();

Wrapped in new Thread like others suggested here - better performance.

Tim Sexton
  • 188
  • 8
  • Seems like you should be able to modify that project and call the toggleFlash(boolean turnLightOn) [located in the SimpleFlashlight / app / src / main / java / com / wolfhorse / simpleflashlight / FlashlightActivity.java file] using a Blink() method like you are attempting above. Let me know if it works. – Tim Sexton Jul 31 '14 at 20:28
  • It doesn't work :-( I was not able to find out the error – user3891554 Aug 01 '14 at 09:52