使用内容URI获取MediaStore.Images.Media._ID

时间:2018-01-05 21:21:27

标签: java android

我刚刚使用相机在相册中保存了一张照片,我的内容是Uri,如下所示:

Uri contentUriOfPhoto = content://com.example.photorq.provider/external_files/Pictures/Photorq/IMG_20180105_231322.jpg

现在我想获得此图片的MediaStore.Images.Media._ID。

这是我一直在尝试使用的内容,但由于某些原因我无法选择列:

public String getIdFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            final String[] columns = { MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID };
            cursor = context.getContentResolver().query(contentUri,  columns, null, null, null);
            if (cursor != null && cursor.getCount()>0) {
                cursor.moveToFirst();
                int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                int dataColumnId = cursor.getColumnIndex( MediaStore.Images.Media._ID);
                Log.v(TAG,""+cursor);
                return cursor.getInt(dataColumnId);
            }

            return -1;

        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

我将以上代码称为: getIdFromUri(contentUriOfPhoto)

我收到一条错误消息,说我选择了0列。我如何查询数据有什么问题吗?

编辑:

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                                currentUserPhoto = savePhotoToExternalStorage();
                                if (currentUserPhoto != null) {
                                    Log.v(TAG, "Will take photo now"+currentUserPhoto.getAbsolutePath()+" "+getApplicationContext().getPackageName());
                                    takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                    currentUserPhotoUri = 
FileProvider.getUriForFile(activity, getApplicationContext().getPackageName() + ".provider", currentUserPhoto);
                                    Log.v(TAG,"My photo is: "+currentUserPhotoUri);
                                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, currentUserPhotoUri);
                                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                                }
                        }

和OnActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            File f = new File(currentUserPhoto.getAbsolutePath());
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);
            Log.v(TAG,"First uri "+contentUri+" second uri "+ currentUserPhotoUri);
            Log.v(TAG,"Saved file image to gallery and id is"+ getIdFromURI(this, currentUserPhotoUri));
        }
    }

以下是创建将保存照片的文件的方法:

private static File savePhotoToExternalStorage(){
        File fileStorageDirectory = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "Photorq");

        if (!fileStorageDirectory.exists()){
            if (!fileStorageDirectory.mkdirs()){
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return new File(fileStorageDirectory.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    }

0 个答案:

没有答案