Android 文件管理】分区存储 ( 修改与删除图片文件 )

网友投稿 933 2022-05-29

文章目录

一、分区存储模式下使用 MediaStore 修改图片

二、分区存储模式下使用 MediaStore 删除图片

三、相关文档资料

Android 分区存储系列博客 :

【Android 文件管理】应用可访问的存储空间 ( 存储空间分类 | 存储空间访问权限 | 分区存储 )

【Android 文件管理】分区存储 ( 分区存储机制 和 文件索引数据 )

【Android 文件管理】分区存储 ( MediaStore 文件操作 )

【Android 文件管理】分区存储 ( 创建与查询图片文件 )

【Android 文件管理】分区存储 ( 修改与删除图片文件 )

在上一篇博客 【Android 文件管理】分区存储 ( 创建与查询图片文件 ) 中 , 使用

MediaStore

在外置存储 SD 卡中的 Pictures 目录中 ,

创建

了 image.jpg 图片文件 , 并进行了

查询 ;

本篇博客讲解使用

MediaStore

修改

,

删除

图片文件操作 ;

一、分区存储模式下使用 MediaStore 修改图片

将 /sdcard/Pictures/image/ 目录下的 image.jpg 修改为 image_update.jpg ;

分区存储机制中 , 删除图片文件 , 不能通过获取其绝对路径进行删除 , 必须先使用

MediaStore 查询到图片文件的 Uri ,

然后通过 Uri 执行 删除 / 修改 图片文件的操作 ;

查询图片 : 查询图片文件的具体原理参考 【Android 文件管理】分区存储 ( 创建与查询图片文件 ) , 不再详细分析 ;

首先 ,

调用 getContentResolver 方法获取 ContentResolver

,

执行 query 查询方法

; 传入

查询的 Uri ,

指定要查询的列 ,

查询语句,

查询参数 ,

排列规则 ,

这 5 5 5 个参数 , 查询结果是 Cursor 对象 ;

// 查询 SQLite 数据库 var cursor = contentResolver.query( // 指定要查询的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查询的列 null, // 指定查询语句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查询参数 arrayOf("image.jpg"), // 排序规则 null )

1

2

3

4

5

6

7

8

9

10

11

12

13

然后 ,

从 Cursor 中获取 MediaStore.Images.Media._ID 字段对应的值

,

通过 ContentUris 的 withAppendedId 方法 , 将 _id 字段值转为 Uri

, 并保存在外部变量中 ;

// 要删除的图片对应的 Uri, 需要先查询出来 var uri: Uri?= null // 先获取该图片在数据库中的 id , 然后通过 id 获取 Uri if (cursor != null && cursor.moveToFirst()){ // 获取第 0 行 _id 所在列的值 var id = cursor.getLong( // 获取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通过 _id 字段获取图片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查询到的 Uri = $uri , 开始准备删除") // 关闭游标 cursor.close() }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

得到了图片的 Uri 地址 , 就可以对图片进行 删除 / 修改 操作了 ; 在 分区存储机制 中 , 只能通过文件的 Uri 地址操作文件的 增 删 查 改 ;

修改图片 : 构造

ContentValues

, 将 display_name 修改成 image_update , 设置 MediaStore.Images.ImageColumns.DISPLAY_NAME 字段对应的值为 “image_update.jpg” , 然后 调用 getContentResolver 方法获取 ContentResolver , 调用

update

方法 , 更新图片 Uri 对应的数据 , 将上述 ContentValues 更新到 Uri 对应的数据库表中 ;

// 修改图片 // 构造 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 结果 row = $row")

1

2

3

4

5

6

7

8

9

查询 并 修改 图片文件代码示例 :

/** * 修改图片 */ fun updateImages(){ // 要删除的图片对应的 Uri, 需要先查询出来 var uri: Uri?= null // 查询 SQLite 数据库 var cursor = contentResolver.query( // 指定要查询的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查询的列 null, // 指定查询语句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查询参数 arrayOf("image.jpg"), // 排序规则 null ) // 先获取该图片在数据库中的 id , 然后通过 id 获取 Uri if (cursor != null && cursor.moveToFirst()){ // 获取第 0 行 _id 所在列的值 var id = cursor.getLong( // 获取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通过 _id 字段获取图片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查询到的 Uri = $uri , 开始准备修改") // 关闭游标 cursor.close() } // 修改图片 // 构造 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 结果 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

日志打印结果 :

查询到的 Uri = content://media/external/images/media/53 , 开始准备修改 修改 uri = content://media/external/images/media/53 结果 row = 1

1

2

文件删除效果 : 在 /sdcard/Pictures/image/ 目录中 , image.jpg 文件已经被修改为 image_update.jpg 文件 ;

二、分区存储模式下使用 MediaStore 删除图片

将 /sdcard/Pictures/image/ 目录下的 image_update.jpg 文件删除 ;

分区存储机制中 , 删除图片文件 , 不能通过获取其绝对路径进行删除 , 必须先使用

MediaStore 查询到图片文件的 Uri ,

然后通过 Uri 执行 删除 / 修改 图片文件的操作 ;

查询图片 : 查询图片文件的具体原理参考 【Android 文件管理】分区存储 ( 创建与查询图片文件 ) , 不再详细分析 ;

首先 ,

调用 getContentResolver 方法获取 ContentResolver

,

执行 query 查询方法

; 传入

查询的 Uri ,

指定要查询的列 ,

查询语句,

查询参数 ,

排列规则 ,

这 5 5 5 个参数 , 查询结果是 Cursor 对象 ;

// 查询 SQLite 数据库 var cursor = contentResolver.query( // 指定要查询的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查询的列 null, // 指定查询语句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查询参数 arrayOf("image_update.jpg"), // 排序规则 null )

1

2

3

4

5

6

7

8

9

10

11

12

13

然后 , 从 Cursor 中获取 MediaStore.Images.Media._ID 字段对应的值 , 通过 ContentUris 的 withAppendedId 方法 , 将 _id 字段值转为 Uri , 并保存在外部变量中 ;

// 要删除的图片对应的 Uri, 需要先查询出来 var uri: Uri?= null // 先获取该图片在数据库中的 id , 然后通过 id 获取 Uri if (cursor != null && cursor.moveToFirst()){ // 获取第 0 行 _id 所在列的值 var id = cursor.getLong( // 获取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通过 _id 字段获取图片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查询到的 Uri = $uri , 开始准备删除") // 关闭游标 cursor.close() }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

得到了图片的 Uri 地址 , 就可以对图片进行 删除 / 修改 操作了 ; 在 分区存储机制 中 , 只能通过文件的 Uri 地址操作文件的 增 删 查 改 ;

删除图片 : 调用 getContentResolver 方法获取 ContentResolver , 直接删除之前查询出的图片 Uri 即可 ;

// 删除图片 var row = contentResolver.delete(uri!!, null, null) Log.i(TAG, "删除 uri = $uri 结果 row = $row")

1

2

3

4

查询 并 删除 图片文件代码示例 :

/** * 删除图片 */ fun deleteImages(){ // 要删除的图片对应的 Uri, 需要先查询出来 var uri: Uri?= null // 查询 SQLite 数据库 var cursor = contentResolver.query( // 指定要查询的 Uri MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // 指定要查询的列 null, // 指定查询语句 "${MediaStore.Images.Media.DISPLAY_NAME}=?", // 指定查询参数 arrayOf("image_update.jpg"), // 排序规则 null ) // 先获取该图片在数据库中的 id , 然后通过 id 获取 Uri if (cursor != null && cursor.moveToFirst()){ // 获取第 0 行 _id 所在列的值 var id = cursor.getLong( // 获取 _id 所在列的索引 cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) ) // 通过 _id 字段获取图片 Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); Log.i(TAG, "查询到的 Uri = $uri , 开始准备删除") // 关闭游标 cursor.close() } // 删除图片 var row = contentResolver.delete(uri!!, null, null) Log.i(TAG, "删除 uri = $uri 结果 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

日志打印结果 :

查询到的 Uri = content://media/external/images/media/53 , 开始准备删除 删除 uri = content://media/external/images/media/53 结果 row = 1

1

2

文件删除效果 : 在 /sdcard/Pictures/image/ 目录中 , image_update.jpg 文件已经被删除 ;

三、相关文档资料

Android 文件处理参考文档 :

数据和文件存储概览 : https://developer.android.google.cn/training/data-storage

访问应用专属文件 : https://developer.android.google.cn/training/data-storage/app-specific#kotlin

保存到共享的存储空间 : https://developer.android.google.cn/training/data-storage/shared

管理存储设备上的所有文件 : https://developer.android.google.cn/training/data-storage/manage-all-files

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

应用安装位置 : https://developer.android.google.cn/guide/topics/data/install-location

Android 存储用例和最佳做法 : 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

【Android 文件管理】分区存储 ( 修改与删除图片文件 )

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

Android

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:软件测试常用工具总结(测试管理、单元测试、接口测试、自动化测试、性能测试、负载测试...)
下一篇:《企业私有云建设指南》一3.2企业私有云资源规划及设计
相关文章