我正在制作一个应用程序,我需要通过按下按钮打开Android手机的手电筒灯。我写了一些代码。但是按下按钮后,指示灯不会亮起。以下是相关代码。请注意,这是在片段内完成的。我尝试了一个具有相同问题的独立应用。
public class Flashlight extends Fragment implements View.OnClickListener {
// in global
private Camera camera;
private Button button;
Button FlashLightBTN
// end global
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.udp_commands, container, false);
FlashLightBTN = (Button) rootView.findViewById(R.id.flashlightBTN);
Context context = getActivity().getApplicationContext();
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
// Log.e("err", "Device has no camera!");
Toast.makeText(getActivity().getApplicationContext(), "Your device doesn't have camera!", Toast.LENGTH_SHORT).show();
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
FlashLightBTN.setTag(1);
FlashLightBTN.setText("Turn Flashlight On");
FlashLightBTN.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Button b = (Button) v;
String buttonText = b.getText().toString();
final int status = (Integer) v.getTag();
if (status == 1) {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
FlashLightBTN.setText("Turn Flashlight Off");
v.setTag(0);
}
if (status == 0) {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
camera.release(); FlashLightBTN.setText("Turn Flashlight On");
v.setTag(1);
}
}
});
// other code
return rootView;
}
}
以下是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andy_xmark1"
android:versionCode="1"
android:versionName="1.0"
android:windowSoftInputMode="stateHidden" >
<!-- Allows access to the flashlight -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.flashlight.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的:
09-04 00:50:25.279: I/Adreno-EGL(11871): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
09-04 00:50:25.299: D/OpenGLRenderer(11871): Enabling debug mode 0
09-04 00:50:25.329: D/dalvikvm(11871): GC_FOR_ALLOC freed 218K, 2% free 16986K/17236K, paused 11ms, total 11ms
09-04 00:50:28.909: D/dalvikvm(11871): GC_FOR_ALLOC freed 387K, 3% free 17113K/17532K, paused 14ms, total 14ms
09-04 00:50:33.659: D/dalvikvm(11871): GC_FOR_ALLOC freed 510K, 4% free 17117K/17660K, paused 15ms, total 15ms
09-04 00:50:35.999: D/AndroidRuntime(11871): Shutting down VM
09-04 00:50:35.999: W/dalvikvm(11871): threadid=1: thread exiting with uncaught exception (group=0x41cf2ba8)
09-04 00:50:36.009: E/AndroidRuntime(11871): FATAL EXCEPTION: main
09-04 00:50:36.009: E/AndroidRuntime(11871): Process: com.example.flashlight, PID: 11871
09-04 00:50:36.009: E/AndroidRuntime(11871): java.lang.RuntimeException: Method called after release()
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.hardware.Camera.native_setParameters(Native Method)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.hardware.Camera.setParameters(Camera.java:1650)
09-04 00:50:36.009: E/AndroidRuntime(11871): at com.example.flashlight.flash$11.onClick(UDP_Commands.java:225)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.view.View.performClick(View.java:4438)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.view.View$PerformClick.run(View.java:18422)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.os.Handler.handleCallback(Handler.java:733)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.os.Handler.dispatchMessage(Handler.java:95)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.os.Looper.loop(Looper.java:136)
09-04 00:50:36.009: E/AndroidRuntime(11871): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-04 00:50:36.009: E/AndroidRuntime(11871): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 00:50:36.009: E/AndroidRuntime(11871): at java.lang.reflect.Method.invoke(Method.java:515)
09-04 00:50:36.009: E/AndroidRuntime(11871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-04 00:50:36.009: E/AndroidRuntime(11871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-04 00:50:36.009: E/AndroidRuntime(11871): at dalvik.system.NativeStart.main(Native Method)
09-04 00:50:37.869: I/Process(11871): Sending signal. PID: 11871 SIG: 9
对我出错的地方提供任何帮助或指导都会非常感激。此外,如果它很重要,我正在使用Nexus 5进行测试。我还从Play商店下载了一个样本手电筒应用程序,并且工作正常,因此硬件很好用。
提前致谢
答案 0 :(得分:0)
在 camera.setParameters(p);
之后添加以下这一行在开启Torch时,您必须添加
camera.startPreview();
在关闭阶段,您必须添加
camera.stopPreview();
camera.release();
打开/关闭手电筒:
添加如下权限: -
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="@string/permlab_flashlight"
android:description="@string/permdesc_flashlight" />
希望它会对你有所帮助。
答案 1 :(得分:0)
看看这个来源,我希望这可以帮助你
https://github.com/survivingwithandroid/Swa-app/tree/master/TorchApp