uri的格式为:content://downloads/all_downloads/101
调用如下:
Uri uri = ContentUris.withAppendedId(Uri.parse("content://downloads/all_downloads"), downloadId);
String path = getDataColumn(uri, null, null);
/**
* 根据uri获取当前路径
*/
public String getDataColumn(Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";//路径保存在downloads表中的_data字段
final String[] projection = {column};
try {
cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
通过withAppendedId方法,为该Uri加上ID
ContentUris.withAppendedId("content://downloads/all_downloads", id);
从路径中获取ID
Uri uri = Uri.parse("content://downloads/all_downloads/75")
long personid = ContentUris.parseId(uri);