1. 从URI中获取真实文件路径
private String uriToRealPath(Uri uri) {
Cursor cursor = managedQuery(uri,
new String[] {MediaStore.Images.Media.DATA},
null,
null,
null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
return path;
}
2. 从真实路径转换为文件URI
Uri uri = Uri.fromFile(new File(filePath));
3. 从真实路径转换为媒体库URI
private Uri pathToMediaUri(String path) {
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = getContentResolver().query(mediaUri,
null,
MediaStore.Images.Media.DISPLAY_NAME + "=" + path.substring(path.lastIndexOf("/") + 1),
null,
null);
cursor.moveToFirst();
Uri uri = ContentUris.withAppendedId(mediaUri, cursor.getLong(0));
return uri;
}
PS: 如果想调用"com.android.camera.action.CROP"裁剪图片,文件URI是用不了的,必须要获取媒体库的URI。