2017-02-03 41 views
1

我從我的應用程序啓動相機拍攝照片。它也可在畫廊中找到。Android獲取最新拍攝照片的路徑

與此有關的常見問題是如何知道照片的路徑。所提出的解決方案是:

  1. 保存路徑自己,並使用EXTRA_OUTPUT
  2. 採取從畫廊中的最後拍攝的照片的路徑發送。

解決方案1 ​​isn't reliable

我努力使解決方案2的工作,有了這個代碼:

public static String getTakenPhotoPath(Context context) { 
    try { 
     String[] projection = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = context.getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       projection, null, null, null); 

     int column_index_data = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToLast(); 

     return cursor.getString(column_index_data); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 

     return null; 
    } 
} 

這不返回的最新照片的路徑;它返回前一個。詢問最新照片的路徑之前,我這樣做:

private void addToGallery(Uri urlType, String path) { 
    if (!Strings.isNullOrEmpty(path)) { 
     ContentValues values = new ContentValues(); 
     // Add the date meta data to ensure the image is added at the front of the gallery 
     values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis()); 
     values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
     values.put(MediaStore.MediaColumns.DATA, path); 

     getContentResolver().insert(urlType, values); 

     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     Uri contentUri = Uri.fromFile(new File(path)); 
     mediaScanIntent.setData(contentUri); 
     sendBroadcast(mediaScanIntent); 
    } 
} 
+0

解決方案2比解決方案1的可靠性差很多。沒有相機應用程序要求'MediaStore'索引的圖像,更不用說及時這樣做了。查看[這個答案](https://stackoverflow.com/a/41945392/115145)爲我推薦的'ACTION_IMAGE_CAPTURE'分類。 – CommonsWare

+0

解決方案1是可靠的。我從來沒有見過它失敗。 – greenapps

+0

@greenapps:您還沒有看到來自各種相機應用程序的Stack Overflow報告問題的所有人員。 'ACTION_IMAGE_CAPTURE'只是沒有經過太多的測試,有些開發人員並沒有閱讀關於'ACTION_IMAGE_CAPTURE'應該如何工作的文檔。看到[從2015年的這個咆哮](https://commonsware.com/blog/2015/06/08/action-image-capture-fallacy.html)瞭解更多。 – CommonsWare

回答

0

你可以把它這樣做:當你打開攝像頭,你可以通過自己的首選地點爲未來捕捉這樣的形象在給定的代碼中。在onActivityResult中,您可以直接使用該圖像路徑供您使用。

File image ; 
    Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
      imagesFolder.mkdirs(); // <---- 
      image = new File(imagesFolder, getFileName()); 
      Uri uriSavedImage = Uri.fromFile(image); 
      photoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
      startActivityForResult(photoCaptureIntent, PICK_FROM_CAMERA); 


    private String getFileName() { 
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 
      Date now = new Date(); 
      String fileName = formatter.format(now) + ".jpg"; 
      return fileName; 
     } 


@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     try { 
      setResult(resultCode, data); 


      switch (requestCode) { 
       case PICK_FROM_CAMERA: 
        try { 
         if (resultCode != RESULT_CANCELED) { 
          Intent upload = new Intent(EditProfile.this, ImageCropingFucAtivity.class); 
          prefs.edit().putBoolean("FromCamera", true).commit(); 
          upload.putExtra(CropImage.IMAGE_PATH, image.toString()); 
          upload.putExtra(CropImage.SCALE, true); 
          upload.putExtra(CropImage.ASPECT_X, 2); 
          upload.putExtra(CropImage.ASPECT_Y, 2); 
          startActivity(upload); 
          imageSelected = true; 
          finish(); 
         } 

        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

        break; 

        } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
+0

這不完全是他的「解決方案1」嗎? – marienke

+0

但它是他的查詢的解決方案,並且我在我的項目上使用它工作正常。 –