Making a simple strobe light out of my camera's flash feature.
I can't figure out why I'm getting an error on lines 31 and 32 for the

Code:

cam.stopPreview();
cam.release();


Here's my activity;

Code:

package com.Strobe;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
       
        for (int i=1;i<1000;i++)
        {
            for (int j=1;j<2;j++)
            {
                Camera cam = Camera.open();     
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
            }
               
                cam.stopPreview();
                cam.release();
        }
    }
}

Here's my .xml layout file;

Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:screenOrientation="portrait"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MainActivity" />
</LinearLayout>
<permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="@string/permlab_flashlight"
             android:description="@string/permdesc_flashlight" />
You didn't say what the error was, so I'm going to guess. The error says that "cam" is not defined, or something like that.

"cam" is defined only inside the inner "for" block, so it's not in scope outside that "for" block. Either move the "Camera cam" declaration outside the inner for loop, or move the cam.* method calls inside the inner for loop.
Your inner for loop isn't doing anything, get rid of it. Also I don't think this will work as you are blocking the UI thread, which might result in nothing happening. You also need to add some delays, otherwise the "strobe" will be over immediately.

You instead need to use a Handler and send delayed messages to yourself.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement