亞寵展、全球?qū)櫸锂a(chǎn)業(yè)風(fēng)向標(biāo)——亞洲寵物展覽會(huì)深度解析
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
然后 ,
從 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)容。