2016-01-14 8 views
7

Voglio fare uno sfondo con la fotocamera su un SurfaceView, e riesco ad Android API inferiore a 23. Anche se in precedenza richiedevo il permesso della fotocamera in app su API 23, il seguente errore ancora appaiono sul mio Nexus 5 con Android 6:Superficie vista con fotocamera non funzionante su Android API 23 (Android 6+)

java.lang.NullPointerException: Tentativo di richiamare il metodo virtuale 'vuoto android.hardware.Camera.setPreviewDisplay (android.view.SurfaceHolder)' su riferimento a oggetto nullo su com.package.name.CameraPreview.surfaceCreated (CameraPreview.java:31)

Il mio codice è:

import android.content.Context; 
import android.hardware.Camera; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

import java.io.IOException; 

/** A basic Camera preview class */ 
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 
    private SurfaceHolder mHolder; 
    private Camera mCamera; 

    public CameraPreview(Context context, Camera camera) { 
     super(context); 
     mCamera = camera;; 

     // Install a SurfaceHolder.Callback so we get notified when the 
     // underlying surface is created and destroyed. 
     mHolder = getHolder(); 
     mHolder.addCallback(this); 
     // deprecated setting, but required on Android versions prior to 3.0 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // The Surface has been created, now tell the activity_hologram where to draw the preview. 
     try { 
      mCamera.setPreviewDisplay(holder); 
      mCamera.startPreview(); 
      mCamera.setDisplayOrientation(90); 
     } catch (IOException e) { 
      Log.d("", "Error setting activity_hologram preview: " + e.getMessage()); 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // empty. Take care of releasing the Camera preview in your activity. 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     // If your preview can change or rotate, take care of those events here. 
     // Make sure to stop the preview before resizing or reformatting it. 

     if (mHolder.getSurface() == null){ 
      // preview surface does not exist 
      return; 
     } 

     // stop preview before making changes 
     try { 
      mCamera.stopPreview(); 
     } catch (Exception e){ 
      // ignore: tried to stop a non-existent preview 
     } 

     // set preview size and make any resize, rotate or 
     // reformatting changes here 

     // start preview with new settings 
     try { 
      mCamera.setPreviewDisplay(mHolder); 
      mCamera.startPreview(); 

     } catch (Exception e){ 
      Log.d("", "Error starting activity_hologram preview: " + e.getMessage()); 
     } 
    } 

} 

L'errore salta su mCamera.setPreviewDisplay (titolare);

Il mio codice attività principale:

public class MainActivity extends AppCompatActivity { 

private static Context mContext; 

private Camera mCamera; 
private CameraPreview mPreview; 

private FrameLayout layout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mContext = getApplicationContext(); 

    layout = (FrameLayout) findViewById(R.id.camera_preview); 

    mCamera = getCameraInstance(); 
    mPreview = new CameraPreview(this, mCamera); 
    layout.addView(mPreview); 
} 


/** A safe way to get an instance of the Camera object. */ 
public static Camera getCameraInstance(){ 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } 
    catch (Exception e){ 
     // Camera is not available (in use or does not exist) 
    } 
    return c; // returns null if activity_hologram is unavailable 
} 

} 

mio manifesta ottenuto:

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

Tutte le idee per risolvere questo problema su Android 6?

Grazie per il vostro tempo, grazie.

+0

Potete per favore indicare quale linea è la linea 31 in CameraPreview.java? – Fildor

+0

mCamera.setPreviewDisplay (titolare); – mmsergi

+0

OK, quindi sono abbastanza sicuro, la mia risposta ti porterà alla fonte di questo. – Fildor

risposta

4

ho segnato il posto problematico in commento:

public static Camera getCameraInstance(){ 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } 
    catch (Exception e){ 
     // Camera is not available (in use or does not exist) 
// >>> HERE is your problem! Do not swallow exceptions silently! 
    } 
    return c; // returns null if activity_hologram is unavailable 
} 

Sembra getCameraInstance restituisce null. Che potrebbe avere due motivi:

  1. Camera.Open() restituisce null
  2. v'è un'eccezione, che non sarà a conoscenza, perché basta silenziosamente ingoiare esso e andare avanti.

un altro punto:

Questa classe è stata sconsigliata a livello di API 21.
Si consiglia di utilizzare la nuova API android.hardware.camera2 per nuove applicazioni.

Da Android Developer Site

+0

grazie per la tua risposta, il codice getCameraInstance() funziona, si blocca quando aggiungo la vista su: layout.addView (mPreview); – mmsergi

+0

@mmsergi Il messaggio di eccezione sais in modo diverso. Quello che ho letto da lì è che si tenta di chiamare setPreviewDisplay in surfaceCreated su un riferimento che è null. Quel riferimento è mCamera. mCamera può essere nullo solo se getCameraInstance restituisce null, vero? Registrare l'eccezione all'interno di getCameraInstance - se è solo per dimostrare che ho torto. Lo ammetterò così immediatamente ma fino ad allora, la mia scommessa è su quel metodo. – Fildor

4

Costruire sulla @ risposta di FILDOR, che mi ha segnalato nella giusta direzione. L'oggetto camera è nullo. Nelle API 23 e successive, vai in info app -> permessi e attiva l'autorizzazione della fotocamera per ottenere correttamente l'oggetto camera.

0

Questa API funziona ancora sulle versioni più recenti di Android, testata su 6.0.1 solo il problema è aggiungere l'autorizzazione Camera e chiedere all'utente di concederlo esplicitamente.

Problemi correlati