1
1
@file:JvmName(" ImageExt" )
2
+ @file:Suppress(" unused" )
2
3
3
4
package com.dede.mediastoredemo
4
5
@@ -17,13 +18,8 @@ import java.io.OutputStream
17
18
18
19
private const val TAG = " ImageExt"
19
20
20
- const val MIME_PNG = " image/png"
21
- const val MIME_JPG = " image/jpg"
22
21
private val ALBUM_DIR = Environment .DIRECTORY_PICTURES
23
22
24
- /* *
25
- * 用于Q以下系统获取图片文件大小来更新[MediaStore.Images.Media.SIZE]
26
- */
27
23
private class OutputFileTaker (var file : File ? = null )
28
24
29
25
/* *
@@ -33,7 +29,7 @@ private class OutputFileTaker(var file: File? = null)
33
29
* @param fileName 文件名。 需要携带后缀
34
30
* @param relativePath 相对于Pictures的路径
35
31
*/
36
- fun File.copyToAlbum (context : Context , fileName : String , relativePath : String? = null ): Uri ? {
32
+ fun File.copyToAlbum (context : Context , fileName : String , relativePath : String? ): Uri ? {
37
33
if (! this .canRead() || ! this .exists()) {
38
34
Log .w(TAG , " check: read file error: $this " )
39
35
return null
@@ -50,11 +46,7 @@ fun File.copyToAlbum(context: Context, fileName: String, relativePath: String? =
50
46
* @param fileName 文件名。 需要携带后缀
51
47
* @param relativePath 相对于Pictures的路径
52
48
*/
53
- fun InputStream.saveToAlbum (
54
- context : Context ,
55
- fileName : String ,
56
- relativePath : String? = null
57
- ): Uri ? {
49
+ fun InputStream.saveToAlbum (context : Context , fileName : String , relativePath : String? ): Uri ? {
58
50
val resolver = context.contentResolver
59
51
val outputFile = OutputFileTaker ()
60
52
val imageUri = resolver.insertMediaImage(fileName, relativePath, outputFile)
@@ -86,7 +78,7 @@ fun Bitmap.saveToAlbum(
86
78
context : Context ,
87
79
fileName : String ,
88
80
relativePath : String? = null,
89
- quality : Int = 75
81
+ quality : Int = 75,
90
82
): Uri ? {
91
83
// 插入图片信息
92
84
val resolver = context.contentResolver
@@ -99,8 +91,7 @@ fun Bitmap.saveToAlbum(
99
91
100
92
// 保存图片
101
93
(imageUri.outputStream(resolver) ? : return null ).use {
102
- val format =
103
- if (fileName.endsWith(" .png" )) Bitmap .CompressFormat .PNG else Bitmap .CompressFormat .JPEG
94
+ val format = fileName.getBitmapFormat()
104
95
this @saveToAlbum.compress(format, quality, it)
105
96
imageUri.finishPending(context, resolver, outputFile.file)
106
97
}
@@ -119,7 +110,7 @@ private fun Uri.outputStream(resolver: ContentResolver): OutputStream? {
119
110
private fun Uri.finishPending (
120
111
context : Context ,
121
112
resolver : ContentResolver ,
122
- outputFile : File ?
113
+ outputFile : File ? ,
123
114
) {
124
115
val imageValues = ContentValues ()
125
116
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q ) {
@@ -128,7 +119,7 @@ private fun Uri.finishPending(
128
119
}
129
120
resolver.update(this , imageValues, null , null )
130
121
// 通知媒体库更新
131
- val intent = Intent (Intent .ACTION_MEDIA_SCANNER_SCAN_FILE , this )
122
+ val intent = Intent (@Suppress( " DEPRECATION " ) Intent .ACTION_MEDIA_SCANNER_SCAN_FILE , this )
132
123
context.sendBroadcast(intent)
133
124
} else {
134
125
// Android Q添加了IS_PENDING状态,为0时其他应用才可见
@@ -137,18 +128,42 @@ private fun Uri.finishPending(
137
128
}
138
129
}
139
130
131
+ private fun String.getBitmapFormat (): Bitmap .CompressFormat {
132
+ val fileName = this .lowercase()
133
+ return when {
134
+ fileName.endsWith(" .png" ) -> Bitmap .CompressFormat .PNG
135
+ fileName.endsWith(" .jpg" ) || fileName.endsWith(" .jpeg" ) -> Bitmap .CompressFormat .JPEG
136
+ fileName.endsWith(" .webp" ) -> if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R )
137
+ Bitmap .CompressFormat .WEBP_LOSSLESS else Bitmap .CompressFormat .WEBP
138
+ else -> Bitmap .CompressFormat .PNG
139
+ }
140
+ }
141
+
142
+ private fun String.getMimeType (): String? {
143
+ val fileName = this .lowercase()
144
+ return when {
145
+ fileName.endsWith(" .png" ) -> " image/png"
146
+ fileName.endsWith(" .jpg" ) || fileName.endsWith(" .jpeg" ) -> " image/jpeg"
147
+ fileName.endsWith(" .webp" ) -> " image/webp"
148
+ fileName.endsWith(" .gif" ) -> " image/gif"
149
+ else -> null
150
+ }
151
+ }
152
+
140
153
/* *
141
154
* 插入图片到媒体库
142
155
*/
143
156
private fun ContentResolver.insertMediaImage (
144
157
fileName : String ,
145
158
relativePath : String? ,
146
- outputFileTaker : OutputFileTaker ? = null
159
+ outputFileTaker : OutputFileTaker ? = null,
147
160
): Uri ? {
148
161
// 图片信息
149
162
val imageValues = ContentValues ().apply {
150
- val mimeType = if (fileName.endsWith(" .png" )) MIME_PNG else MIME_JPG
151
- put(MediaStore .Images .Media .MIME_TYPE , mimeType)
163
+ val mimeType = fileName.getMimeType()
164
+ if (mimeType != null ) {
165
+ put(MediaStore .Images .Media .MIME_TYPE , mimeType)
166
+ }
152
167
val date = System .currentTimeMillis() / 1000
153
168
put(MediaStore .Images .Media .DATE_ADDED , date)
154
169
put(MediaStore .Images .Media .DATE_MODIFIED , date)
@@ -166,7 +181,8 @@ private fun ContentResolver.insertMediaImage(
166
181
// 高版本不用查重直接插入,会自动重命名
167
182
} else {
168
183
// 老版本
169
- val pictures = Environment .getExternalStoragePublicDirectory(ALBUM_DIR )
184
+ val pictures =
185
+ @Suppress(" DEPRECATION" ) Environment .getExternalStoragePublicDirectory(ALBUM_DIR )
170
186
val saveDir = if (relativePath != null ) File (pictures, relativePath) else pictures
171
187
172
188
if (! saveDir.exists() && ! saveDir.mkdirs()) {
@@ -192,7 +208,7 @@ private fun ContentResolver.insertMediaImage(
192
208
// 保存路径
193
209
val imagePath = imageFile.absolutePath
194
210
Log .v(TAG , " save file: $imagePath " )
195
- put(MediaStore .Images .Media .DATA , imagePath)
211
+ put(@Suppress( " DEPRECATION " ) MediaStore .Images .Media .DATA , imagePath)
196
212
}
197
213
outputFileTaker?.file = imageFile// 回传文件路径,用于设置文件大小
198
214
collection = MediaStore .Images .Media .EXTERNAL_CONTENT_URI
@@ -220,8 +236,8 @@ private fun ContentResolver.queryMediaImage28(imagePath: String): Uri? {
220
236
// 查询是否已经存在相同图片
221
237
val query = this .query(
222
238
collection,
223
- arrayOf(MediaStore .Images .Media ._ID , MediaStore .Images .Media .DATA ),
224
- " ${MediaStore .Images .Media .DATA } == ?" ,
239
+ arrayOf(MediaStore .Images .Media ._ID , @Suppress( " DEPRECATION " ) MediaStore .Images .Media .DATA ),
240
+ " ${@Suppress( " DEPRECATION " ) MediaStore .Images .Media .DATA } == ?" ,
225
241
arrayOf(imagePath), null
226
242
)
227
243
query?.use {
0 commit comments