簡體   English   中英

連接相機時發生錯誤:0

[英]An error occurred while connecting to camera: 0

我正在制作一個手電筒應用程序,該應用程序在用戶搖動手機或按應用程序上的按鈕時啟動。 當我打開應用程序並按該應用程序中的按鈕時,當我回到家並搖動手機時,該應用程序仍按預期運行,但是當我打開該應用程序並按該按鈕時不起作用。 它在我的貓咪里說

“ CameraBase(20450):連接到相機時發生錯誤:0相機錯誤。無法打開。錯誤:(20450):無法連接到相機服務”

這是我的主要Activity.java

public class MainActivity extends Activity {
 private ToggleButton togle;
 private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    Parameters params;
    private ShakeListener mShaker;
    MediaPlayer mp;
    ImageView anime;
@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    anime = (ImageView) findViewById(R.id.Animation);

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

    if (!hasFlash) {
        // device doesn't support flash
        // Show alert message and close the application
        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                .create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // closing the application
                finish();   }
        });
        alert.show();
        return;}


    getCamera();
    togle = (ToggleButton) findViewById(R.id.ToggleButton01);

    togle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
              boolean checked = ((ToggleButton) v).isChecked();
               if (checked){
                   turnOffFlash(); }

               else{

                   turnOnFlash(); 
               }
                  }
        });
    mShaker = new ShakeListener(this);
    mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
         public void onShake()
         { if (!isFlashOn) {
             Toast.makeText(MainActivity.this, "On" , Toast.LENGTH_SHORT).show();


         turnOnFlash();
         }
         else{
               turnOffFlash();

               Toast.makeText(MainActivity.this, "Off" , Toast.LENGTH_SHORT).show();   
         } }
       });
        }
private void getCamera() {
    // TODO Auto-generated method stub
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    } }

private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
        playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;
        anime.setImageResource(R.drawable.anim);
        anime.post(new Runnable() {
            @Override
            public void run() {
                AnimationDrawable frameAnimation =
                    (AnimationDrawable) anime.getDrawable(); 
                frameAnimation.start();
            }
        });

        // changing button/switch image
    }  } 
 private void turnOffFlash() {
        if (isFlashOn) {
            if (camera == null || params == null) {
                return;
            }
            // play sound
            playSound();

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;
            anime.setImageResource(R.drawable.off);
            // changing button/switch image
        }
    }

private void playSound() {
    // TODO Auto-generated method stub
     if(isFlashOn){
            mp = MediaPlayer.create(MainActivity.this, R.raw.off1);
        }else{
            mp = MediaPlayer.create(MainActivity.this, R.raw.on1);
        }
        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.release();
            }
        }); 
        mp.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

       }

我的Xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg"
tools:context="com.shakylight.MainActivity" >

<ToggleButton
    android:id="@+id/ToggleButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textOff=""
     android:textOn=""
    android:layout_marginTop="170dp"
    android:background="@drawable/light"
    android:checked="true" />

<ImageView
    android:id="@+id/Animation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
     android:contentDescription="Animation"
     android:background="@drawable/off"
    android:layout_marginTop="148dp"
    />

聽起來您的應用無法正確釋放相機,請嘗試添加:

camera.setPreviewCallback(null);
camera.release();
camera = null;

進入您的turnFlash

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM