2016-06-30 19 views
0

在SO上也有這樣的類似問題,但沒有一個符合我的具體情況。來自onPictureTaken回調的decodeByteArray數據返回null

我正在執行使用官方tutorial的攝像頭控件。

我拍攝的方法是調用mCamera.takePicture(null, null, this),然後從onPictureTaken接收回調。我遇到的問題是我似乎無法將decodeByteArray的數據傳遞到onPictureTakendecodeByteArray繼續返回null。看來字節數組的格式是無效的。

我試圖通過FileOutputStream照原樣保存字節數組,就像在教程中一樣,然後通過decodeFile再次讀取字節,它仍然返回null。我試圖捕獲異常但沒有被捕獲。

請注意,我可以保存照片就好了。我無法從傳遞的參數中讀取字節。我似乎無法弄清楚這一點。

這裏是我的代碼:

@Override 
public void onPictureTaken(byte[] data, Camera camera) 
{ 
    FileOutputStream out = null; 
    String fileName = Photo.getDirectory(this) + "/Confirm_" + mDateFormatter.format(new Date()) + ".jpg"; 

    try 
    { 
     File temp = new File(fileName); 
     out = new FileOutputStream(temp); 
     out.write(data); 
     out.close(); 

     try 
     { 
      // This always returns NULL 
      BitmapFactory.Options option = new BitmapFactory.Options(); 
      option.inJustDecodeBounds = true; 
      option.inSampleSize = 2; 
      Bitmap bitmap = BitmapFactory.decodeFile(fileName, option); 

      if(bitmap == null) ; // TRUE 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

     /* 
     Camera.Parameters param = mCamera.getParameters(); 
     List<Camera.Size> sizes = param.getSupportedPictureSizes(); 
     Bitmap bitmap = BitmapFactory.decodeFile(fileName); 

     for(Camera.Size s : sizes) 
     { 
      Log.i(PhotoApplication.TAG, "Size : " + s.width + " x " + s.height); 
     } 
     */ 

     Intent intent = new Intent(); 
     intent.putExtra(EXTRA_FILENAME, temp.getAbsolutePath() + "/" + temp.getName()); 
     setResult(RESULT_OK, intent); 

     Toast.makeText(this, "Saving as : " + temp.getAbsolutePath() + "/" + temp.getName(), Toast.LENGTH_LONG).show(); 
    } 
    catch(FileNotFoundException e) 
    { 

    } 
    catch(IOException e) 
    { 

    } 
    finally 
    { 
     try 
     { 
      if(out != null) 
       out.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    finish(); 
} 

回答

1

問題是

option.inJustDecodeBounds = true; 

根據該文件

如果設置爲true,解碼器將返回null(沒有位圖) ,但out ...字段仍將被設置,允許調用者查詢位圖而不必爲其像素分配內存。

刪除它或將其設置爲false如果您想解碼位圖供以後使用。

+0

該死的......這是對我的一記耳光。非常感謝你! –