-1

I have emulator but is super slow(AMD CPU)

Ok so here is code

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Button btn = (Button)findViewById(R.id.btnpower);
private Camera cam1;
Camera.Parameters params;
private boolean isOn;

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

    cam1 = Camera.open();
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isOn) {
                params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                cam1.setParameters(params);
                cam1.stopPreview();
                isOn = false;
                btn.setBackgroundResource(R.drawable.off);


            } else {
                params = cam1.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam1.setParameters(params);
                cam1.stopPreview();
                isOn = true;
                btn.setBackgroundResource(R.drawable.on);
            }
        }

    });
}

}

David
  • 2,987
  • 1
  • 29
  • 35
  • Show the stacktrace with crash – ruX Oct 09 '16 at 10:11
  • Welcome to Stack Overflow! Please review [how to ask](http://stackoverflow.com/help/how-to-ask) to improve your question. Post the errors you have received. Be as specific as possible as it will lead to better answers. – David Oct 09 '16 at 10:13

1 Answers1

0

You need to declare button inside onCreateand below setContentView. Furthermore, in your else condition , instead of cam1.stopPreview(); you should put cam1.startPreview();.In your if condition,make sure you put cam1.release(); below cam1.stopPreview();. Make sure you've added

<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.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />

this in your manifest.xml

  • It should be inside **onCreate** because that's where you're placing the view. Like this, `setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.btnpower); ` – Sushant Belsare Oct 09 '16 at 10:59
  • No,same problem – Dusan Culum Oct 09 '16 at 11:16
  • Can you post the stack trace? – Sushant Belsare Oct 09 '16 at 11:18
  • Stack trace is logcat,no? – Dusan Culum Oct 09 '16 at 11:40
  • Yes! I just went through your stack trace and I think the problem you're getting might be because of `E/memtrack: Couldn't load memtrack module`. This error usually occurs either because of emulator configurations or because the project has organization problem. I'll suggest you go through this [link](http://stackoverflow.com/a/22745487/6943705) and see if it helps you solve the problem. – Sushant Belsare Oct 09 '16 at 11:54
  • I think it supports API 15+ but you can always check using this `Boolean isFlashAvailable = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);` – Sushant Belsare Oct 09 '16 at 12:13
  • I tested on real device(Samsung galaxy s4) so i think problem is in organization i will google it. – Dusan Culum Oct 09 '16 at 12:13