Android获取最新拍摄照片的路径

时间:2017-02-03 17:35:07

标签: android android-camera-intent

我正从我的应用程序启动相机拍照。它也可以在画廊中使用。

与此相关的常见问题是如何知道照片的路径。建议的解决方案是:

  1. 自行保存路径,然后使用EXTRA_OUTPUT
  2. 发送
  3. 从画廊中最后拍摄的照片中选择路径。
  4. 解决方案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);
        }
    }
    

1 个答案:

答案 0 :(得分: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();
        }

    }