DocumentsContract.getDocumentId(uri)

时间:2016-04-21 03:26:41

标签: android

我正在尝试获取我保存的图像的文档ID,我将图像文件保存到设备中的以下外部存储器中。

file:///storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg

查询后查询

public String checkIfImageIsRotated(Uri image_uri){

    Log.i(LOG_TAG, ">>>>> START ");
    int rotation =0;
    String[] selection = new String[] { MediaStore.Images.Media._ID,
                                        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                                        MediaStore.Images.Media.DATA,
                                        MediaStore.Images.Media.DATE_TAKEN,
                                        MediaStore.Images.Media.ORIENTATION};

    String selectionArgs = MediaStore.Images.Media.BUCKET_DISPLAY_NAME +  " = ?";
    String[] args = new String[]{FOLDER_NAME};
    String sortOrder = MediaStore.Images.Media.DATE_TAKEN + " DESC";

    ContentResolver content = mContext.getContentResolver();
    Cursor mediaCursor = content.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                        selection ,
                                        selectionArgs,
                                        args,
                                sortOrder);

    //select of columns
    //MediaStore.Images.ImageColumns.ORIENTATION
    //MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
    if (mediaCursor != null && mediaCursor.getCount() !=0 ) {
        if(mediaCursor.moveToNext()){
            Log.i(LOG_TAG, ">>>>> media cursor not null ");
            String orientation = mediaCursor.getString(4);
            String bucket_display_name = mediaCursor.getString(1);
            image_data_path = mediaCursor.getString(2);
            Log.i(LOG_TAG, ">>>>> rotation -  " + orientation + ", bucket display name - " + bucket_display_name + ", data - " + image_data_path);
        }

        mediaCursor.close();
    }

    Log.i(LOG_TAG, ">>>>> END ");
    return image_data_path;
}

image_data_path是

/storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg

现在,当我尝试执行DocumentsContract.getDocumentId(uri)时,其中uri是顶部提到的文件或光标返回的image_data_path,在这两种情况下我都收到以下错误。

Caused by: java.lang.IllegalArgumentException: Invalid URI: file:///storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg

Caused by: java.lang.IllegalArgumentException: Invalid URI: /storage/emulated/0/Pictures/BeautifulFaces/kvh_20160420T225141.jpg

无法理解为什么Document.getDocumentId无法解析Uri。

由于

0 个答案:

没有答案