UriUtil.java 文件源码

java
阅读 36 收藏 0 点赞 0 评论 0

项目:leisure-glance 作者:
/**
    * Gets the corresponding path to a file from the given URI
    *
    * @param uri The URI to find the file path from
    * @param contentResolver The content resolver to use to perform the query.
    * @return the file path as a string
    */
public static String getPathFromUri(@NonNull Uri uri, ContentResolver contentResolver) {
    String filePath = null;
    String scheme = uri.getScheme();

    if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        String[] filePathColumn = { MediaStore.MediaColumns.DATA };
        Cursor cursor = contentResolver.query(uri, filePathColumn, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            cursor.close();
        }
    } else {
           filePath = uri.getPath();
       }
    return filePath;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号