2016-03-20 5 views
1

更新:私は自分の質問を編集して作業コードを追加しました。私は私の問題を解決しました。私の最新/最後の投稿を参照してください。トーチアプリの問題

注:私はLinuxのコマンドラインからコンパイルします。

私はトーチのアプリを持っています。私は仕事をしようとしていますが、私は運がないです。誰かが私が逃して説明していることを指摘することはできますか?とても感謝しております!

問題なくコンパイルできます。私は問題なしにプッシュすることができます。私は問題なしでオンとオフのボタンを切り替えることができます。私は、私が置いたトーストでこれを見ることができます。基本的にすべての機能が正しく呼び出されるようですが、バックカメラのLEDライトは点灯しません。私は5.1.1でNexus 6を使用し、Linuxで開発しています。また、私はここで言及する価値があると思うが、私はLinuxボックスとトーチのwoks上でサードパーティのトーチアプリをコンパイルすることができます。

主な活動:

package com.flash.light; 

import android.util.Log; 
import android.os.Bundle; 
import android.view.View; 
import android.app.Activity; 
import android.widget.Toast; 
import android.widget.Button; 
import android.content.Context; 
import android.hardware.Camera; 
import android.view.SurfaceView; 
import android.view.SurfaceHolder; 
import android.view.View.OnClickListener; 
import android.content.pm.PackageManager; 
import android.hardware.Camera.Parameters; 

public class FlashLight extends Activity implements SurfaceHolder.Callback { 

    private Camera mCam; 
    private boolean isFlashOn; 
    private Parameters params; 
    private Button flashLight; 
    private boolean hasCameraFlash; 
    private SurfaceView surfaceView; 
    private SurfaceHolder surfaceHolder; 

    private static final String TAG = FlashLight.class.getSimpleName(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    surfaceView = (SurfaceView)findViewById(R.id.preview); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    hasCameraFlash = getApplicationContext().getPackageManager() 
     .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 

    if(!(hasCameraFlash)) { 
     Toast.makeText(this, "Camera does not have flash feature.", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    else { 
     getCamera(); 
     Toast.makeText(this, "Opened Camera.", Toast.LENGTH_LONG).show(); 
    } 

    isFlashOn = false; 
    flashLight = (Button)findViewById(R.id.flashLight); 
    flashLight.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
     try { 
      toggle(); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
     } 

    }); 
    } 

    public void toggle() { 
    try { 
     if(!(isFlashOn)) { 
     flashLightOn(); 
     flashLight.setText("OFF"); 
     Toast.makeText(this, "flashLightOn()", Toast.LENGTH_LONG).show(); 
     } 
     else { 
     flashLightOff(); 
     flashLight.setText("ON"); 
     Toast.makeText(this, "flashLightOff()", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public void getCamera() { 
     try { 
     mCam = Camera.open(); 
     params = mCam.getParameters(); 
     } 
     catch(Exception e) { 
     e.printStackTrace(); 
     } 
    } 

    public void flashLightOn() { 
    if(mCam == null) { 
     Toast.makeText(this, "Camera not found.", Toast.LENGTH_LONG).show(); 
     return; 
    } 

    if(!(isFlashOn)) { 
     params = mCam.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
     mCam.setParameters(params); 
     mCam.startPreview(); 
     isFlashOn = true; 
     Toast.makeText(this, "isFlashOn = true", Toast.LENGTH_LONG).show(); 
    } 
    } 

    public void flashLightOff() { 
    try { 
     if(isFlashOn) { 
     params = mCam.getParameters(); 
     params.setFlashMode(Parameters.FLASH_MODE_OFF); 
     mCam.setParameters(params); 
     mCam.stopPreview(); 
     mCam.release(); 
     isFlashOn = false; 
     Toast.makeText(this, "isFlashOn = false", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    Log.d(TAG,"surfaceChanged()"); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
    try { 
     mCam.setPreviewDisplay(holder); 
     Toast.makeText(this, "Setting preview.", Toast.LENGTH_LONG).show(); 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
    Toast.makeText(this, "Preview stopped.", Toast.LENGTH_LONG).show(); 
    mCam.stopPreview(); 
    } 

} 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="FlashLight"/> 

<SurfaceView 
    android:id="@+id/preview" 
    android:layout_width="1dp" 
    android:layout_height="1dp"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="ON" 
    android:id="@+id/flashLight"/> 

</RelativeLayout> 

マニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.flash.light" 
     android:versionCode="1" 
     android:versionName="1.0"> 

    <uses-permission android:name="android.permission.CAMERA"/> 
    <uses-permission android:name="android.permission.FLASHLIGHT"/> 
    <uses-feature android:name="android.hardware.camera.autofocus" /> 
    <uses-feature android:name="android.hardware.camera.flash" /> 
    <uses-feature android:name="android.hardware.camera"/> 

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 
     <activity android:name="FlashLight" 
        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> 
+0

チェック - ここ

私はgithubのを経由してトーチアプリを働いていますか? 'context.getPackageManager()。hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);' – AADProgramming

+0

市場からトーチアプリを使用できるようになって以来、この問題はわかりません。私はgitからサードパーティ製のアプリケーションをコンパイルし、トーチも機能しました。 – Aguevara

+0

OK、テスト用に使用しているデバイスの名前も記載することをお勧めします。一部のSamsungデバイスに関する既知の問題がいくつかあります。ここをクリックしてください:http://stackoverflow.com/questions/6068803/how-to-turn-on-camera-flash-light-programmatically-in-android – AADProgramming

答えて