Android获取相册图片的路径 4.4版本前后的变化

网友投稿 1049 2022-05-30

两个问题:

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

Android获取相册中图片的路径 4.4版本前后的变化

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小时内删除侵权内容。

上一篇:ROS1和ROS2如何选?(机器人操作系统2021)ROS2极简总结-新增概念
下一篇:博文|你还在用python写API,赶紧体验全新curl、jq的API!
相关文章