Android獲取相冊中圖片的路徑 4.4版本前后的變化
兩個問題:
1. 通過調用系統Action,從圖庫中選擇圖片,并展示到界面上
2. API19前后獲取相冊圖片路徑
詳見(僅客戶端代碼),請移步:本人GITHUB
Intent intent = new Intent(); // Set an explicit MIME data type. intent.setType("image/*"); // Set the general action to be performed. intent.setAction(Intent.ACTION_GET_CONTENT); // callBack startActivityForResult(intent,1);
1
2
3
4
5
6
7
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Uri uri = data.getData(); ContentResolver cr =this.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); /* 將Bitmap設定到ImageView */ iv_local_pic.setImageBitmap(bitmap); int sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK); Log.d("sdkVersion:", String.valueOf(sdkVersion)); Log.d("KITKAT:", String.valueOf(Build.VERSION_CODES.KITKAT)); if (sdkVersion >= 19) { // 或者 android.os.Build.VERSION_CODES.KITKAT這個常量的值是19 path = uri.getPath();//5.0直接返回的是圖片路徑 Uri.getPath is : /document/image:46 ,5.0以下是一個和數據庫有關的索引值 System.out.println("path:" + path); // path_above19:/storage/emulated/0/girl.jpg 這里才是獲取的圖片的真實路徑 path = getPath_above19(Upload_HttpUrlConnection_Activity.this, uri); System.out.println("path_above19:" + path); } else { path = getFilePath_below19(uri); } } catch (FileNotFoundException e) { Log.e("Exception", e.getMessage(), e); } } super.onActivityResult(requestCode, resultCode, data); }
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
/** * API19以下獲取圖片路徑的方法 * @param uri */ private String getFilePath_below19(Uri uri) { //這里開始的第二部分,獲取圖片的路徑:低版本的是沒問題的,但是sdk>19會獲取不到 String[] proj = {MediaStore.Images.Media.DATA}; //好像是android多媒體數據庫的封裝接口,具體的看Android文檔 Cursor cursor = getContentResolver().query(uri, proj, null, null, null); //獲得用戶選擇的圖片的索引值 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); System.out.println("***************" + column_index); //將光標移至開頭 ,這個很重要,不小心很容易引起越界 cursor.moveToFirst(); //最后根據索引值獲取圖片路徑 結果類似:/mnt/sdcard/DCIM/Camera/IMG_20151124_013332.jpg String path = cursor.getString(column_index); System.out.println("path:" + path); return path; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** * APIlevel 19以上才有 * 創建項目時,我們設置了最低版本API Level,比如我的是10, * 因此,AS檢查我調用的API后,發現版本號不能向低版本兼容, * 比如我用的“DocumentsContract.isDocumentUri(context, uri)”是Level 19 以上才有的, * 自然超過了10,所以提示錯誤。 * 添加 @TargetApi(Build.VERSION_CODES.KITKAT)即可。 * * @param context * @param uri * @return */ @TargetApi(Build.VERSION_CODES.KITKAT) public static String getPath_above19(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[]{ split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); }
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Android
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。