我已经创建了一种可能的方法,以便我可以通过一个简单的列表检测哪个参数适合每个手机。现在,当我运行它时,粉碎,
public void turnOn() {
就在这一行:List<String> flashMods = params.getSupportedFlashModes();
if (flashMods.contains(android.hardware.Camera.Parameters.FLASH_MODE_AUTO)) {
params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
mCamera.setParameters(params);
mCamera.startPreview();
on = true;
Toast.makeText(getApplicationContext(),
"2",
Toast.LENGTH_SHORT).show();
}
if(flashMods.contains(android.hardware.Camera.Parameters.FLASH_MODE_ON)) {
params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
mCamera.setParameters(params);
mCamera.startPreview();
on = true;
Toast.makeText(getApplicationContext(),
"3",
Toast.LENGTH_SHORT).show();
}
if(flashMods.contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH)) {
params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
on = true;
Toast.makeText(getApplicationContext(),
"4",
Toast.LENGTH_SHORT).show();
}
if(flashMods.contains(android.hardware.Camera.Parameters.FLASH_MODE_RED_EYE)) {
params = mCamera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_RED_EYE);
mCamera.setParameters(params);
mCamera.startPreview();
on = true;
Toast.makeText(getApplicationContext(),
"5",
Toast.LENGTH_SHORT).show();
}
}
}
提前致谢!
答案 0 :(得分:0)
您是否启用了“CAMERA”权限?
您是否已指定params对象 - 它可能为null。
我这样做(在Mono / C#中,而不是Java,我害怕):
private void SetUpCamera()
{
if (m_camera == null)
m_camera = Android.Hardware.Camera.Open();
var holder = m_surface.Holder;
holder.AddCallback(this);
holder.SetType(Android.Views.SurfaceType.PushBuffers);
SetCameraDisplayOrientation();
GetSurfaceSize(m_camera.GetParameters());
GetFlashModes(m_camera.GetParameters());
}
然后:
private void GetFlashModes(Android.Hardware.Camera.Parameters parameters)
{
IList<String> flashModes = parameters.SupportedFlashModes;
m_flashModes = new List<string>();
if (flashModes == null)//not supported
return;
foreach (string mode in flashModes)
m_flashModes.Add(mode);
if (m_currentMode != null)//mode has been saved, need to set it to this mode
SetFlashMode(m_currentMode);
else
m_currentMode = GetFlashMode();
}