解决4.4以上系统无法根据Uri正确获取图片路径的问题

当需要获取一张手机里的图片时,我们会调起图片浏览器选择图片,图片浏览器会返回图片的Uri,然后根据Uri就可以得到图片的路径,这段代码在4.4一下的系统是没有问题的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 得到当前选中的图片的地址
*/
private String getFilePathFromContentUri(Uri selectedPicUri) {
String filePath = "";
try {
if ("file".equalsIgnoreCase(selectedPicUri.getScheme())) {
filePath = selectedPicUri.getPath();
} else {
String[] filePathColumn = {MediaColumns.DATA};
Cursor c = null;
try {
c = getContentResolver().query(selectedPicUri, filePathColumn, null, null, null);
if (c.moveToFirst()) {
filePath = c.getString(c.getColumnIndex(filePathColumn[0]));
}
} finally {
if (c != null && !c.isClosed()) {
c.close();
}
}
}
} catch (Exception e) {
filePath = "";
DebugUtil.exception(TAG, e);
}
return filePath;
}

然而在4.4的系统上,得到的路径是空的,是因为这句getContentResolver().query(selectedPicUri, filePathColumn, null, null, null);,由于Uri的格式在4.4以上的系统发生了改变,所有需要特殊处理一下,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public static String getPhotoPathFromContentUri(Context context, Uri uri) {
String photoPath = "";
if(context == null || uri == null) {
return photoPath;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if(isExternalStorageDocument(uri)) {
String [] split = docId.split(":");
if(split.length >= 2) {
String type = split[0];
if("primary".equalsIgnoreCase(type)) {
photoPath = SdHelper.SD_DIR + File.separator + split[1];
}
}
}
else if(isDownloadsDocument(uri)) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
photoPath = getDataColumn(context, contentUri, null, null);
}
else if(isMediaDocument(uri)) {
String[] split = docId.split(":");
if(split.length >= 2) {
String type = split[0];
Uri contentUris = null;
if("image".equals(type)) {
contentUris = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
else if("video".equals(type)) {
contentUris = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
else if("audio".equals(type)) {
contentUris = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = new String[] { split[1] };
photoPath = getDataColumn(context, contentUris, selection, selectionArgs);
}
}
}
else if("file".equalsIgnoreCase(uri.getScheme())) {
photoPath = uri.getPath();
}
else {
photoPath = getDataColumn(context, uri, null, null);
}
return photoPath;
}
private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
String column = MediaStore.Images.Media.DATA;
String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null && !cursor.isClosed())
cursor.close();
}
return null;
}

这样就没有问题了.