2015-05-25 17 views
5

Come NullPointerException per alcuni telefoni nel metodo OnPictureTaken. Sto chiamando il metodo takePicture in questo modo.Errore nel metodo onPictureTaken nella telecamera Android

mCamera.takePicture(shutterCallback, null, mPicture); 

mio OnPictureTaken Metodo

private Camera.PictureCallback mPicture = new Camera.PictureCallback() { 


     public void onPictureTaken(byte[] data, Camera camera) { 

      Bitmap bitmap = null; 

      if(flashOff) { 
       resetCamOnFlashOFF(); 
      } 
      else { 
       resetCamOnFlashOn(); 
      } 
      File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
      File dir = new File(sdDir.getAbsolutePath() + "/Camera2/"); 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
      String fileName = "/IMG_" + timeStamp + ".jpeg"; 
      boolean mkDir = dir.mkdirs(); 
      Log.d("Saving Photo","Created the directory" +mkDir); 
      ExifInterface exif; 
      try { 
       exif = new ExifInterface(fileName); 
       exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation); 
       exif.saveAttributes(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


      try { 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
       bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); 
       int reqHeight = options.outHeight; 
       int reqWidth = options.outWidth; 

       options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

       options.inJustDecodeBounds = false; 
       bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); 
      }catch (OutOfMemoryError e){ 
       e.printStackTrace(); 
       System.gc(); 
      } 
      Matrix matrix = new Matrix(); 

      switch(orientation){ 
       case ExifInterface.ORIENTATION_NORMAL : matrix.postRotate(0); 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_90 : matrix.postRotate(90); 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180 : matrix.postRotate(180); 
        break; 
       default: matrix.postRotate(270); 
        break; 
      } 
      assert bitmap != null; 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, 
        bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
      File outFile = new File(dir, fileName); 


      try { 
       FileOutputStream outStream = new FileOutputStream(outFile); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outStream); 
       bitmap.recycle(); 
       outStream.write(data); 
       outStream.flush(); 
       outStream.close(); 
       refreshGallery(outFile); 
      } catch (IOException e) { 
       e.printStackTrace(); 

      } 

      mPreview.safeToTakePicture = true; 

     } 
    }; 

Solo su alcuni telefoni sto errore ottenendo come NullPointerException

Dettagli errore:

NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 
    com.myDomain.camera2.r.onPictureTaken in SCamera.java:482 
    android.hardware.Camera$EventHandler.handleMessage in Camera.java:1142 
    android.os.Handler.dispatchMessage in Handler.java:102 
    android.os.Looper.loop in Looper.java:145 
    android.app.ActivityThread.main in ActivityThread.java:6141 
    java.lang.reflect.Method.invoke in Method.java:-2 
    java.lang.reflect.Method.invoke in Method.java:372 
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run in ZygoteInit.java:1399 
    com.android.internal.os.ZygoteInit.main in ZygoteInit.java:1194 

risposta

0

eliminare il codice

options.inJustDecodeBounds = true; 

e fare attenzione a OOM. Oppure prova questo:

options.inJustDecodeBounds = true; 
options.inSampleSize = 5; 
options.inJustDecodeBounds = false; 
Problemi correlati