我试图在Android中打开Nexus5闪光灯,但我在logcat中发现了一个错误。 是否可以打开它?
我的代码在这里:
活动Java
public class MainActivity extends Activity {
Camera camera;
Parameters parameters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (switch1.isChecked()) {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(parameters);
Toast.makeText(getApplicationContext(),
"Turning on FlashLight..", Toast.LENGTH_LONG)
.show();
} else {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
Toast.makeText(getApplicationContext(),
"Turning off FlashLight..", Toast.LENGTH_LONG)
.show();
}
}
});
}
}
我正在使用这些权限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera.autofocus" />
05-07 10:08:26.970: W/CameraBase(19606): An error occurred while connecting to camera: 0
05-07 10:08:26.970: D/AndroidRuntime(19606): Shutting down VM
05-07 10:08:26.970: W/dalvikvm(19606): threadid=1: thread exiting with uncaught exception (group=0x41682ba8)
05-07 10:08:26.970: E/AndroidRuntime(19606): FATAL EXCEPTION: main
05-07 10:08:26.970: E/AndroidRuntime(19606): Process: com.systelligence.androidflashlight, PID: 19606
05-07 10:08:26.970: E/AndroidRuntime(19606): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.systelligence.androidflashlight/com.systelligence.androidflashlight.MainActivity}: java.lang.RuntimeException: Fail to connect to camera service
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread.access$900(ActivityThread.java:135)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 10:08:26.970: E/AndroidRuntime(19606): at android.os.Looper.loop(Looper.java:136)
答案 0 :(得分:0)
我会使用以下代码。在我的Nexus手机上完美运行。我需要实现SurfaceView来实现这个目标:
public class ConcertFragment extends Fragment implements SurfaceHolder.Callback {
ToggleButton btnFlashlight;
View rootView;
private Camera cam;
private boolean hasFlash;
boolean hasCamera;
boolean isFlashOn;
@Override
public void onStart() {
super.onStart();
SurfaceView preview = (SurfaceView)getView().findViewById(R.id.background);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}
@Override
public void onPause(){
super.onPause();
turnOffFlash();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getCamera();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_concert, container, false);
hasFlash = getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(getActivity()).create();
alert.setTitle("No Flash");
alert.setMessage("Sorry, device is not flash supported.");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
}
btnFlashlight = (ToggleButton) rootView.findViewById(R.id.toggleButton);
btnFlashlight.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
return rootView;
}
private void turnOnFlash() {
if (!isFlashOn) {
if (cam == null || params == null) {
return;
}
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(params);
cam.startPreview();
isFlashOn = true;
}
}
private void turnOffFlash() {
if (isFlashOn) {
if (cam == null || params == null) {
return;
}
params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();
cam.release();
isFlashOn = false;
}
}
// Get the camera
private void getCamera() {
if (cam != null) {
try {
cam = Camera.open();
params = cam.getParameters();
cam.startPreview();
hasCamera = true;
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (cam != null) {
cam.stopPreview();
cam.setPreviewCallback(null);
cam.release();
cam = null;
btnFlashlight.setChecked(false);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (cam == null) {
cam = Camera.open();
params = cam.getParameters();
try {
cam.setPreviewDisplay(holder);
} catch (IOException e) {
cam.release();
cam = null;
}
}
}