0

我在Android活動中嘗試使用自定義文件名保存圖像時有以下代碼。安卓相機在玻璃上的意圖 - 在自定義文件路徑中保存圖像

我試圖從這個帖子頂答案複製代碼如下:Android - Taking photos and saving them with a custom name to a custom destination via Intent

我的代碼如下:

//camera stuff 
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 

    //folder stuff 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
    imagesFolder.mkdirs(); 
    File image = new File(imagesFolder, "QR_" + timeStamp + ".png"); 
    Uri uriSavedImage = Uri.fromFile(image); 

    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    Log.d(LOG_TAG, "writing photo to: " + uriSavedImage.toString()); 
    startActivityForResult(imageIntent, 1); 

當我運行這個活動,它顯示在屏幕上的畫面,我必須點擊觸摸板接受(這是在玻璃上運行),並打印在DDMS如下:

writing photo to: file:///mnt/sdcard/MyImages/QR_20140106_181934.jpg 

然而,當我檢查,在文件目錄EXPLO通過DDMS rer,它是空的。它成功創建了目錄MyImages,但沒有保存該目錄中的任何文件。

相反,它已經創造了我只是把圖像以下文件:/mnt/sdcard/DCIM/Camera/20140106_181935_635.jpg

所以它抓住了正確的圖像,但只是忽略了,我告訴它保存它。任何想法我做錯了什麼?

回答

4

該文件可能不是當你看着它被完全寫入,所以使用FileWatcher像一個在GDK docs

private static final int TAKE_PICTURE_REQUEST = 1; 

private void takePicture() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, TAKE_PICTURE_REQUEST); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) { 
     String picturePath = data.getStringExtra(
       CameraManager.EXTRA_PICTURE_FILE_PATH); 
     processPictureWhenReady(picturePath); 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 

private void processPictureWhenReady(final String picturePath) { 
    final File pictureFile = new File(picturePath); 

    if (pictureFile.exists()) { 
     // The picture is ready; process it. 
    } else { 
     // The file does not exist yet. Before starting the file observer, you 
     // can update your UI to let the user know that the application is 
     // waiting for the picture (for example, by displaying the thumbnail 
     // image and a progress indicator). 

     final File parentDirectory = pictureFile.getParentFile(); 
     FileObserver observer = new FileObserver(parentDirectory.getPath()) { 
      // Protect against additional pending events after CLOSE_WRITE is 
      // handled. 
      private boolean isFileWritten; 

      @Override 
      public void onEvent(int event, String path) { 
       if (!isFileWritten) { 
        // For safety, make sure that the file that was created in 
        // the directory is actually the one that we're expecting. 
        File affectedFile = new File(parentDirectory, path); 
        isFileWritten = (event == FileObserver.CLOSE_WRITE 
          && affectedFile.equals(pictureFile)); 

        if (isFileWritten) { 
         stopWatching(); 

         // Now that the file is ready, recursively call 
         // processPictureWhenReady again (on the UI thread). 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           processPictureWhenReady(picturePath); 
          } 
         }); 
        } 
       } 
      } 
     }; 
     observer.startWatching(); 
    } 
} 
相關問題