方法:
/**
* 把content uri转为 文件路径
*
* @param contentUri 要转换的content uri
* @param contentResolver 解析器
* @return
*/
public static String getFilePathFromContentUri(Uri contentUri,
ContentResolver contentResolver) {
String filePath;
String[] filePathColumn = {MediaStore.MediaColumns.DATA};
Cursor cursor = contentResolver.query(contentUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
使用:
File tempFile = new File( getFilePathFromContentUri(uri, getActivity().getContentResolver()));