Android獲取相冊圖片的路徑 4.4版本前后的變化

      網友投稿 1211 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

      Android獲取相冊中圖片的路徑 4.4版本前后的變化

      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小時內刪除侵權內容。

      上一篇:基于MindSpore框架wide&deep模型的實戰CTR體驗
      下一篇:ApiPost V3 如何設置一個變量?
      相關文章
      亚洲av无码无在线观看红杏| 国产成人精品亚洲精品| 亚洲中文字幕久久精品无码VA| 久久精品亚洲综合| 亚洲色精品88色婷婷七月丁香 | 亚洲日本视频在线观看| 久久91亚洲精品中文字幕| 亚洲产国偷V产偷V自拍色戒| 亚洲人成色7777在线观看| 国产亚洲精品自在线观看| 亚洲人成网站在线观看青青| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 国产精品无码亚洲精品2021| 亚洲精品国产av成拍色拍| 亚洲AV无码之国产精品| 国产精品亚洲色图| 亚洲男女内射在线播放| 亚洲一级Av无码毛片久久精品| 久久精品国产亚洲5555| 亚洲伊人久久综合中文成人网| 狠狠亚洲婷婷综合色香五月排名| 亚洲中文字幕无码永久在线| 亚洲熟妇丰满多毛XXXX| 亚洲国产精品一区第二页| 亚洲福利在线视频| 亚洲最大在线观看| 亚洲13又紧又嫩又水多| 亚洲欧美日韩中文字幕一区二区三区| 亚洲精品9999久久久久无码| 国产成人亚洲毛片| 国产成人精品曰本亚洲79ren| 亚洲精品蜜桃久久久久久| 亚洲人成电影在线天堂| 亚洲人成高清在线播放| 亚洲中文无码永久免费| 日韩精品成人亚洲专区| 亚洲日韩中文无码久久| 中文字幕亚洲精品| 亚洲 暴爽 AV人人爽日日碰| 在线视频亚洲一区| 亚洲毛片αv无线播放一区|