Android 文件管理】分區(qū)存儲(chǔ) ( 修改與刪除圖片文件 )

      網(wǎng)友投稿 1170 2022-05-29

      文章目錄

      一、分區(qū)存儲(chǔ)模式下使用 MediaStore 修改圖片

      二、分區(qū)存儲(chǔ)模式下使用 MediaStore 刪除圖片

      三、相關(guān)文檔資料

      Android 分區(qū)存儲(chǔ)系列博客 :

      【Android 文件管理】應(yīng)用可訪問(wèn)的存儲(chǔ)空間 ( 存儲(chǔ)空間分類 | 存儲(chǔ)空間訪問(wèn)權(quán)限 | 分區(qū)存儲(chǔ) )

      【Android 文件管理】分區(qū)存儲(chǔ) ( 分區(qū)存儲(chǔ)機(jī)制 和 文件索引數(shù)據(jù) )

      【Android 文件管理】分區(qū)存儲(chǔ) ( MediaStore 文件操作 )

      【Android 文件管理】分區(qū)存儲(chǔ) ( 創(chuàng)建與查詢圖片文件 )

      【Android 文件管理】分區(qū)存儲(chǔ) ( 修改與刪除圖片文件 )

      在上一篇博客 【Android 文件管理】分區(qū)存儲(chǔ) ( 創(chuàng)建與查詢圖片文件 ) 中 , 使用

      MediaStore

      在外置存儲(chǔ) SD 卡中的 Pictures 目錄中 ,

      創(chuàng)建

      了 image.jpg 圖片文件 , 并進(jìn)行了

      查詢 ;

      本篇博客講解使用

      MediaStore

      修改

      ,

      刪除

      圖片文件操作 ;

      一、分區(qū)存儲(chǔ)模式下使用 MediaStore 修改圖片

      將 /sdcard/Pictures/image/ 目錄下的 image.jpg 修改為 image_update.jpg ;

      分區(qū)存儲(chǔ)機(jī)制中 , 刪除圖片文件 , 不能通過(guò)獲取其絕對(duì)路徑進(jìn)行刪除 , 必須先使用

      MediaStore 查詢到圖片文件的 Uri ,

      然后通過(guò) Uri 執(zhí)行 刪除 / 修改 圖片文件的操作 ;

      查詢圖片 : 查詢圖片文件的具體原理參考 【Android 文件管理】分區(qū)存儲(chǔ) ( 創(chuàng)建與查詢圖片文件 ) , 不再詳細(xì)分析 ;

      首先 ,

      調(diào)用 getContentResolver 方法獲取 ContentResolver

      ,

      執(zhí)行 query 查詢方法

      ; 傳入

      查詢的 Uri ,

      指定要查詢的列 ,

      查詢語(yǔ)句,

      查詢參數(shù) ,

      排列規(guī)則 ,

      這 5 5 5 個(gè)參數(shù) , 查詢結(jié)果是 Cursor 對(duì)象 ;

      // 查詢 SQLite 數(shù)據(jù)庫(kù) var cursor = contentResolver.query( // 指定要查詢的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查詢的列 null, // 指定查詢語(yǔ)句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查詢參數(shù) arrayOf("image.jpg"), // 排序規(guī)則 null )

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      然后 ,

      【Android 文件管理】分區(qū)存儲(chǔ) ( 修改與刪除圖片文件 )

      從 Cursor 中獲取 MediaStore.Images.Media._ID 字段對(duì)應(yīng)的值

      ,

      通過(guò) ContentUris 的 withAppendedId 方法 , 將 _id 字段值轉(zhuǎn)為 Uri

      , 并保存在外部變量中 ;

      // 要?jiǎng)h除的圖片對(duì)應(yīng)的 Uri, 需要先查詢出來(lái) var uri: Uri?= null // 先獲取該圖片在數(shù)據(jù)庫(kù)中的 id , 然后通過(guò) id 獲取 Uri if (cursor != null && cursor.moveToFirst()){ // 獲取第 0 行 _id 所在列的值 var id = cursor.getLong( // 獲取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通過(guò) _id 字段獲取圖片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查詢到的 Uri = $uri , 開始準(zhǔn)備刪除") // 關(guān)閉游標(biāo) cursor.close() }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      得到了圖片的 Uri 地址 , 就可以對(duì)圖片進(jìn)行 刪除 / 修改 操作了 ; 在 分區(qū)存儲(chǔ)機(jī)制 中 , 只能通過(guò)文件的 Uri 地址操作文件的 增 刪 查 改 ;

      修改圖片 : 構(gòu)造

      ContentValues

      , 將 display_name 修改成 image_update , 設(shè)置 MediaStore.Images.ImageColumns.DISPLAY_NAME 字段對(duì)應(yīng)的值為 “image_update.jpg” , 然后 調(diào)用 getContentResolver 方法獲取 ContentResolver , 調(diào)用

      update

      方法 , 更新圖片 Uri 對(duì)應(yīng)的數(shù)據(jù) , 將上述 ContentValues 更新到 Uri 對(duì)應(yīng)的數(shù)據(jù)庫(kù)表中 ;

      // 修改圖片 // 構(gòu)造 ContentValues var contentValues: ContentValues = ContentValues(); // 將 display_name 修改成 image_update contentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "image_update.jpg") // 修改文件名稱 var row = contentResolver.update(uri!!, contentValues, null, null) Log.i(TAG, "修改 uri = $uri 結(jié)果 row = $row")

      1

      2

      3

      4

      5

      6

      7

      8

      9

      查詢 并 修改 圖片文件代碼示例 :

      /** * 修改圖片 */ fun updateImages(){ // 要?jiǎng)h除的圖片對(duì)應(yīng)的 Uri, 需要先查詢出來(lái) var uri: Uri?= null // 查詢 SQLite 數(shù)據(jù)庫(kù) var cursor = contentResolver.query( // 指定要查詢的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查詢的列 null, // 指定查詢語(yǔ)句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查詢參數(shù) arrayOf("image.jpg"), // 排序規(guī)則 null ) // 先獲取該圖片在數(shù)據(jù)庫(kù)中的 id , 然后通過(guò) id 獲取 Uri if (cursor != null && cursor.moveToFirst()){ // 獲取第 0 行 _id 所在列的值 var id = cursor.getLong( // 獲取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通過(guò) _id 字段獲取圖片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查詢到的 Uri = $uri , 開始準(zhǔn)備修改") // 關(guān)閉游標(biāo) cursor.close() } // 修改圖片 // 構(gòu)造 ContentValues var contentValues: ContentValues = ContentValues(); // 將 display_name 修改成 image_update contentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "image_update.jpg") // 修改文件名稱 var row = contentResolver.update(uri!!, contentValues, null, null) Log.i(TAG, "修改 uri = $uri 結(jié)果 row = $row") } /**

      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

      日志打印結(jié)果 :

      查詢到的 Uri = content://media/external/images/media/53 , 開始準(zhǔn)備修改 修改 uri = content://media/external/images/media/53 結(jié)果 row = 1

      1

      2

      文件刪除效果 : 在 /sdcard/Pictures/image/ 目錄中 , image.jpg 文件已經(jīng)被修改為 image_update.jpg 文件 ;

      二、分區(qū)存儲(chǔ)模式下使用 MediaStore 刪除圖片

      將 /sdcard/Pictures/image/ 目錄下的 image_update.jpg 文件刪除 ;

      分區(qū)存儲(chǔ)機(jī)制中 , 刪除圖片文件 , 不能通過(guò)獲取其絕對(duì)路徑進(jìn)行刪除 , 必須先使用

      MediaStore 查詢到圖片文件的 Uri ,

      然后通過(guò) Uri 執(zhí)行 刪除 / 修改 圖片文件的操作 ;

      查詢圖片 : 查詢圖片文件的具體原理參考 【Android 文件管理】分區(qū)存儲(chǔ) ( 創(chuàng)建與查詢圖片文件 ) , 不再詳細(xì)分析 ;

      首先 ,

      調(diào)用 getContentResolver 方法獲取 ContentResolver

      ,

      執(zhí)行 query 查詢方法

      ; 傳入

      查詢的 Uri ,

      指定要查詢的列 ,

      查詢語(yǔ)句,

      查詢參數(shù) ,

      排列規(guī)則 ,

      這 5 5 5 個(gè)參數(shù) , 查詢結(jié)果是 Cursor 對(duì)象 ;

      // 查詢 SQLite 數(shù)據(jù)庫(kù) var cursor = contentResolver.query( // 指定要查詢的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查詢的列 null, // 指定查詢語(yǔ)句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查詢參數(shù) arrayOf("image_update.jpg"), // 排序規(guī)則 null )

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      然后 , 從 Cursor 中獲取 MediaStore.Images.Media._ID 字段對(duì)應(yīng)的值 , 通過(guò) ContentUris 的 withAppendedId 方法 , 將 _id 字段值轉(zhuǎn)為 Uri , 并保存在外部變量中 ;

      // 要?jiǎng)h除的圖片對(duì)應(yīng)的 Uri, 需要先查詢出來(lái) var uri: Uri?= null // 先獲取該圖片在數(shù)據(jù)庫(kù)中的 id , 然后通過(guò) id 獲取 Uri if (cursor != null && cursor.moveToFirst()){ // 獲取第 0 行 _id 所在列的值 var id = cursor.getLong( // 獲取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通過(guò) _id 字段獲取圖片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查詢到的 Uri = $uri , 開始準(zhǔn)備刪除") // 關(guān)閉游標(biāo) cursor.close() }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      得到了圖片的 Uri 地址 , 就可以對(duì)圖片進(jìn)行 刪除 / 修改 操作了 ; 在 分區(qū)存儲(chǔ)機(jī)制 中 , 只能通過(guò)文件的 Uri 地址操作文件的 增 刪 查 改 ;

      刪除圖片 : 調(diào)用 getContentResolver 方法獲取 ContentResolver , 直接刪除之前查詢出的圖片 Uri 即可 ;

      // 刪除圖片 var row = contentResolver.delete(uri!!, null, null) Log.i(TAG, "刪除 uri = $uri 結(jié)果 row = $row")

      1

      2

      3

      4

      查詢 并 刪除 圖片文件代碼示例 :

      /** * 刪除圖片 */ fun deleteImages(){ // 要?jiǎng)h除的圖片對(duì)應(yīng)的 Uri, 需要先查詢出來(lái) var uri: Uri?= null // 查詢 SQLite 數(shù)據(jù)庫(kù) var cursor = contentResolver.query( // 指定要查詢的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查詢的列 null, // 指定查詢語(yǔ)句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查詢參數(shù) arrayOf("image_update.jpg"), // 排序規(guī)則 null ) // 先獲取該圖片在數(shù)據(jù)庫(kù)中的 id , 然后通過(guò) id 獲取 Uri if (cursor != null && cursor.moveToFirst()){ // 獲取第 0 行 _id 所在列的值 var id = cursor.getLong( // 獲取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通過(guò) _id 字段獲取圖片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查詢到的 Uri = $uri , 開始準(zhǔn)備刪除") // 關(guān)閉游標(biāo) cursor.close() } // 刪除圖片 var row = contentResolver.delete(uri!!, null, null) Log.i(TAG, "刪除 uri = $uri 結(jié)果 row = $row") }

      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

      日志打印結(jié)果 :

      查詢到的 Uri = content://media/external/images/media/53 , 開始準(zhǔn)備刪除 刪除 uri = content://media/external/images/media/53 結(jié)果 row = 1

      1

      2

      文件刪除效果 : 在 /sdcard/Pictures/image/ 目錄中 , image_update.jpg 文件已經(jīng)被刪除 ;

      三、相關(guān)文檔資料

      Android 文件處理參考文檔 :

      數(shù)據(jù)和文件存儲(chǔ)概覽 : https://developer.android.google.cn/training/data-storage

      訪問(wèn)應(yīng)用專屬文件 : https://developer.android.google.cn/training/data-storage/app-specific#kotlin

      保存到共享的存儲(chǔ)空間 : https://developer.android.google.cn/training/data-storage/shared

      管理存儲(chǔ)設(shè)備上的所有文件 : https://developer.android.google.cn/training/data-storage/manage-all-files

      分享文件 : https://developer.android.google.cn/training/secure-file-sharing

      應(yīng)用安裝位置 : https://developer.android.google.cn/guide/topics/data/install-location

      Android 存儲(chǔ)用例和最佳做法 : https://developer.android.google.cn/training/data-storage/use-cases

      FileProvider : https://developer.android.google.cn/reference/androidx/core/content/FileProvider

      博客源碼 :

      GitHub : https://github.com/han1202012/File

      CSDN : https://download.csdn.net/download/han1202012/18935612

      Android

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:軟件測(cè)試常用工具總結(jié)(測(cè)試管理、單元測(cè)試、接口測(cè)試、自動(dòng)化測(cè)試、性能測(cè)試、負(fù)載測(cè)試...)
      下一篇:《企業(yè)私有云建設(shè)指南》一3.2企業(yè)私有云資源規(guī)劃及設(shè)計(jì)
      相關(guān)文章
      亚洲国产成人片在线观看无码 | 精品丝袜国产自在线拍亚洲| 亚洲欧美综合精品成人导航| 久久亚洲中文字幕精品一区| 亚洲性一级理论片在线观看| 亚洲熟女综合色一区二区三区| 国产亚洲综合一区柠檬导航| 国产亚洲情侣一区二区无码AV| 亚洲中文字幕日本无线码| 亚洲高清专区日韩精品| 亚洲色中文字幕无码AV| 国产亚洲综合视频| 亚洲一级免费毛片| 亚洲国产精品久久久久网站| 亚洲人午夜射精精品日韩| 亚洲AV日韩综合一区尤物| 亚洲一区二区三区写真| 亚洲大码熟女在线观看| 亚洲国产成人久久三区| 亚洲AV日韩精品久久久久久| 伊人久久精品亚洲午夜| 亚洲精品国产精品乱码不99| 亚洲精品一级无码鲁丝片| 亚洲乱亚洲乱妇24p| 亚洲AV成人一区二区三区观看| 亚洲ts人妖网站| 亚洲一区二区观看播放| 亚洲Av无码国产一区二区| 国产亚洲精品国产福利在线观看 | 亚洲性无码一区二区三区| 国产精品亚洲一区二区在线观看| 亚洲ts人妖网站| 亚洲GV天堂无码男同在线观看| 亚洲成a人片在线观看日本麻豆| 四虎亚洲精品高清在线观看| 亚洲AV无码专区在线观看成人| 亚洲国产精品不卡毛片a在线| 亚洲熟妇丰满多毛XXXX| 亚洲男人天堂2017| 亚洲一区二区三区四区视频| 亚洲国产精品成人AV在线|