手电筒不会开启

时间:2015-11-14 04:04:54

标签: java android

您好我跟着我跟着教程here并写出了代码,我测试了它,但手电筒没有开启,我无法弄清楚出了什么问题。 我把那里有声音的部分遗漏了,因为我更喜欢那里没有声音。

package net.netne.benpaterson35;
import android.app.*;
import android.os.*;
import android.hardware.*;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity 
{

Button mainButton1;

private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;

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

    mainButton1 = (Button) findViewById(R.id.mainButton1);

    hasFlash = getApplicationContext().getPackageManager().
        hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
            .create();
        alert.setTitle("Sorry");
        alert.setMessage("This device doesn't have flash a flash light");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which){
                    finish();
                }
            });
            alert.show();
            return;
    }

    getCamera();
    mainButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            if (isFlashOn){
                turnOffFlash();}
        else {
            turnOnFlash();
        }

    };

});

}

private void getCamera(){
  if (camera == null){
    try {
        camera = Camera.open();
        params = camera.getParameters();
    } catch (RuntimeException e) {
        Log.e("Failed to open camera", e.getMessage());
    }

    }
 }
private void turnOnFlash() {
  if (!isFlashOn){
    if (camera == null || params == null){
        return;
    }
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
    isFlashOn = true;
}
}

private void turnOffFlash() {
    if (isFlashOn){
        if (camera == null || params == null) {
        return;
        }
     params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
      camera.stopPreview();
        isFlashOn = false;
        }
     }

@Override
protected void onDestroy()
    {
        super.onDestroy();
   }

@Override
  protected void onPause()
 {
      super.onPause();

     turnOffFlash();
  }

@Override
      protected void onRestart()
{
          super.onRestart();
}
  @Override
       protected void onResume()
{
                super.onResume();
               if(hasFlash)
                turnOnFlash();
}

@Override
protected void onStart()
 {
        super.onStart();
        getCamera();
  }

@Override
protected void onStop()
  {
        super.onStop();

    if (camera != null) {
    camera.release();
    camera = null;
    }
}
}

感谢阅读:)

2 个答案:

答案 0 :(得分:0)

如果您正在使用新设备,您所遵循的代码将不适用于较新的设备我通过遵循此代码使其在Lollipop设备上运行试一试

mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
   mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
   // Ignore
}
mCam.startPreview();

答案 1 :(得分:0)

你添加了

吗?
python splitprog.py - - < input.txt > output.txt

到您的AndroidManifest?

非常怀疑你没有添加权限。