4

我有一個非常奇怪的錯誤,試圖通過意圖拍照。 活動代碼(一點點簡化):在onActivityResult中用照相機意圖空置一段時間的圖片文件

private void startCapture() throws IOException { 
    // Create output file 
    final File photoFile = makePhotoFile(); 
    _photoPath = photoFile.getAbsolutePath(); 

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    /* IMPORTANT NOTE TO READERS 
    * As of Android N (which went out shortly after I posted this question), 
    * you cannot use Uri.fromFile this way anymore. 
    * You should use a FileProvider. */ 
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 

    if (cameraIntent.resolveActivity(getPackageManager()) != null) { 
     startActivityForResult(cameraIntent, REQUEST_CODE_IMAGE_CAPTURE); 
    } 
    else { 
     deleteCurrentPhoto(); 
    } 
} 

private File makePhotoFile() throws IOException { 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date()); 
    String imageFileName = "MyPhoto_" + timeStamp + "_"; 
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    return File.createTempFile(imageFileName, ".jpg", storageDir); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if(requestCode == REQUEST_CODE_IMAGE_CAPTURE) { 
     if(resultCode == RESULT_OK) { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      final byte[] buffer = new byte[2048]; 
      int read; 
      byte[] photoBytes; 
      try(FileInputStream fis = new FileInputStream(_photoPath)) { 

       for(int i=0; i<10; i++) { // Always working after first iteration though 
        if((read = fis.read(buffer)) <= 0) { 
         // Why does this get printed once ?? 
         Log.w("my.package.my.app", "Empty for now..."); 
        } 
        else { 
         Log.w("my.package.my.app", "Working now !"); 
         bos.write(buffer, 0, read); 
         break; 
        } 
       } 

       while((read = fis.read(buffer)) >= 0) { 
        bos.write(buffer, 0, read); 
       } 

       photoBytes = bos.toByteArray(); 
      } // Catch clauses removed for simplicity 

      // Everything working OK after that (photoBytes decodes fine with the BitmapFactory) 
     } 
    } 
} 

和日誌輸出:

03-01 16:32:34.139 23414-23414/my.package.my.app W /我的。 package.my.app: 現在爲空... 03-01 16:32:34.139 23414-23414/my.package.my.app W/my.package.my.app:立即開始工作!

正如您所看到的,在拍攝照片後的onActivityResult中,第一次調用FileInputStream.read時該文件爲空...然後可以正確讀取它! 我認爲,當相機意圖返回到調用活動時,該文件將被寫入。是否有某種延遲?我也很驚訝地看到它在for循環中只有一次迭代後總是工作。

請注意,如果我在onActivityResult的開頭放置了一個斷點,它會延遲執行一點,並且一切正常(第一次嘗試時文件被正確讀取)。

我正在使用股票相機應用程序在Android 6.0.1下的股票Nexus 6P。


重要編輯:作爲Android的N,你應該像我一樣以前使用的FileProvider代替Uri.fromFile。見this blog postthese explanations from the Android team

+1

'File.createTempFile'。不要創建該文件。你唯一需要的是一個文件名。一個文件路徑。相機應用程序將創建該文件。所以重命名你的函數來創建新的文件名。 – greenapps

+0

你應該記錄'read'的值。它是0還是-1?或者將聲明分成兩個== 0和<0. – greenapps

+0

不創建文件解決了問題,該指南也創建了一個臨時文件:https://developer.android.com/training/camera/photobasics.html(請參閱「保存完整尺寸的照片」一節。如果您有時間作爲答案發布,我可以接受它(還有,爲什麼我會看到這種行爲?),否則我會自己做。 – personne3000

回答

2

File.createTempFile.不要創建該文件。你唯一需要的是一個文件名。一個文件路徑。相機應用程序將創建該文件。所以重命名你的函數來創建新的文件名。

+0

您也可能想要確保您創建的文件名是唯一的,方法'File.createTempFile()'可用於強制執行碰撞抵抗你的文件名創建算法thm,按照[Android拍攝照片的文檔](https://developer.android.com/training/camera/photobasics.html)。 – Paolone

相關問題