我正在练习并尝试更换 HTC 背面的手电筒(手电筒)。但是我无法访问相机,我在清单中添加了以下内容:
<uses-permission android:name="android.permission.FLASHLIGHT" android:permissionGroup="android.permission-group.HARDWARE_CONTROLS" android:protectionLevel="normal"/> <uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera.flash" />
它可能非常简单和愚蠢,但谷歌和 StackOverflow 没有给我解决方案,所以谁能告诉我我做错了什么?这是我的活动文件:
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.util.Log;
import java.util.List;
public class FlashlightActivity extends Activity {
private Camera mCamera;
/** Called when the activity is first created, just a titlescreen. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton titlescreen = (ImageButton)findViewById(R.id.titlescreen);
titlescreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
TurnFlashOff();
if (mCamera==null){
finish();}
else {
Intent go = new Intent(TurnMeOnFlashlightActivity.this,TMO_App.class);
startActivity(go);
}
}
});
Log.d("TurnMeOn", "startpage");
}
private void TurnFlashOff(){
if(mCamera != null){
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.release();
mCamera = null;
} else {
Toast.makeText(TurnMeOnFlashlightActivity.this, R.string.FlashNoSupport, Toast.LENGTH_LONG).show();
mCamera.release();
mCamera = null;
}
} else {
Toast.makeText(TurnMeOnFlashlightActivity.this, R.string.FlashNoSupport, Toast.LENGTH_LONG).show();
}
}
}