Je travaille sur une application dans laquelle je dois pouvoir allumer la lampe de poche d'un téléphone Android en appuyant sur le bouton. J'ai écrit du code. Cependant, lorsque vous appuyez sur le bouton, la lumière ne s'allume pas. Voici le code pertinent. Veuillez noter que ceci est fait dans un fragment. J'ai essayé une application autonome avec les mêmes problèmes.Android Activer le bouton de la lampe de poche par programmation
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;
}
}
Ci-dessous mon fichier manifeste:
<?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
Toute aide ou des conseils sur l'endroit où je me trompe serait grandement apprécié. Aussi, si cela est important, je suis en train de tester cela avec un Nexus 5. J'ai également téléchargé un exemple d'application de lampe de poche à partir du Playstore, et cela fonctionne très bien, donc le matériel est bon à faire.
Merci à l'avance pour
Déposez votre Logcat. qu'est-ce que "FlashLightBTN"? – SilentKiller
où vous avez initialisé le 'FlashLightBTN'? – SilentKiller