Domanda

I've been trying to get the LED flashlight come on on my Galaxy Nexus, using several different solutions on StackOverflow:

LED flashlight on Galaxy Nexus controllable by what API?

Opening Flashlight of Galaxy Nexus

But I'm still getting this error, and I cannot see why:

Camera: app passed NULL surface

This is my code (or rather the code from other solutions edited slightly):

package com.quinny898.doorcontrol;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class DoorControlDoing extends Activity implements Callback {
public Camera mCamera;

  /** Called when the activity is first created. */

  public void onCreate(Bundle savedInstanceState) {
      setContentView(R.layout.dialog_surface);
      SurfaceView preview = (SurfaceView) findViewById(R.id.surface_view);
      SurfaceHolder mHolder = preview.getHolder();
      mHolder.addCallback(this);


      super.onCreate(savedInstanceState);
      setContentView(R.layout.dialog_surface);
      Camera mCamera = Camera.open();
      Parameters params = mCamera.getParameters();
      params.setFlashMode(Parameters.FLASH_MODE_TORCH);
      mCamera.setParameters(params);      
      mCamera.startPreview();
      try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }




public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {}

public void surfaceCreated(SurfaceHolder holder) {
    setContentView(R.layout.dialog_surface);
    SurfaceView preview = (SurfaceView) findViewById(R.id.surface_view);
      SurfaceHolder mHolder = preview.getHolder();
    mHolder = holder;
    try {
        mCamera.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    setContentView(R.layout.dialog_surface);
    SurfaceView preview = (SurfaceView) findViewById(R.id.surface_view);
      SurfaceHolder mHolder = preview.getHolder();
    mCamera.stopPreview();
    mHolder = null;
}
}

And this is the layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <SurfaceView
    android:id="@+id/surface_view"
    android:layout_width="1px"
    android:layout_height="1px"/>
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/hi"/>
</LinearLayout>

I cannot see what is wrong here, all the IDs are correct and it inflates the layout fine, so why is it "NULL"?

Thanks for any help!

È stato utile?

Soluzione

@Override

 public void surfaceCreated(SurfaceHolder holder) {

    try {
        camera.setPreviewDisplay(holder);
    } catch (Exception e) {

        e.printStackTrace();
    }
}
camera is
camera=Camera.open();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top