Skip to content

Commit f6f0002

Browse files
author
shhu
committed
优化文件类型获取
1 parent c203853 commit f6f0002

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/dede/mediastoredemo/ImageExt.kt

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@file:JvmName("ImageExt")
2+
@file:Suppress("unused")
23

34
package com.dede.mediastoredemo
45

@@ -17,13 +18,8 @@ import java.io.OutputStream
1718

1819
private const val TAG = "ImageExt"
1920

20-
const val MIME_PNG = "image/png"
21-
const val MIME_JPG = "image/jpg"
2221
private val ALBUM_DIR = Environment.DIRECTORY_PICTURES
2322

24-
/**
25-
* 用于Q以下系统获取图片文件大小来更新[MediaStore.Images.Media.SIZE]
26-
*/
2723
private class OutputFileTaker(var file: File? = null)
2824

2925
/**
@@ -33,7 +29,7 @@ private class OutputFileTaker(var file: File? = null)
3329
* @param fileName 文件名。 需要携带后缀
3430
* @param relativePath 相对于Pictures的路径
3531
*/
36-
fun File.copyToAlbum(context: Context, fileName: String, relativePath: String? = null): Uri? {
32+
fun File.copyToAlbum(context: Context, fileName: String, relativePath: String?): Uri? {
3733
if (!this.canRead() || !this.exists()) {
3834
Log.w(TAG, "check: read file error: $this")
3935
return null
@@ -50,11 +46,7 @@ fun File.copyToAlbum(context: Context, fileName: String, relativePath: String? =
5046
* @param fileName 文件名。 需要携带后缀
5147
* @param relativePath 相对于Pictures的路径
5248
*/
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? {
5850
val resolver = context.contentResolver
5951
val outputFile = OutputFileTaker()
6052
val imageUri = resolver.insertMediaImage(fileName, relativePath, outputFile)
@@ -86,7 +78,7 @@ fun Bitmap.saveToAlbum(
8678
context: Context,
8779
fileName: String,
8880
relativePath: String? = null,
89-
quality: Int = 75
81+
quality: Int = 75,
9082
): Uri? {
9183
// 插入图片信息
9284
val resolver = context.contentResolver
@@ -99,8 +91,7 @@ fun Bitmap.saveToAlbum(
9991

10092
// 保存图片
10193
(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()
10495
this@saveToAlbum.compress(format, quality, it)
10596
imageUri.finishPending(context, resolver, outputFile.file)
10697
}
@@ -119,7 +110,7 @@ private fun Uri.outputStream(resolver: ContentResolver): OutputStream? {
119110
private fun Uri.finishPending(
120111
context: Context,
121112
resolver: ContentResolver,
122-
outputFile: File?
113+
outputFile: File?,
123114
) {
124115
val imageValues = ContentValues()
125116
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
@@ -128,7 +119,7 @@ private fun Uri.finishPending(
128119
}
129120
resolver.update(this, imageValues, null, null)
130121
// 通知媒体库更新
131-
val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, this)
122+
val intent = Intent(@Suppress("DEPRECATION") Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, this)
132123
context.sendBroadcast(intent)
133124
} else {
134125
// Android Q添加了IS_PENDING状态,为0时其他应用才可见
@@ -137,18 +128,42 @@ private fun Uri.finishPending(
137128
}
138129
}
139130

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+
140153
/**
141154
* 插入图片到媒体库
142155
*/
143156
private fun ContentResolver.insertMediaImage(
144157
fileName: String,
145158
relativePath: String?,
146-
outputFileTaker: OutputFileTaker? = null
159+
outputFileTaker: OutputFileTaker? = null,
147160
): Uri? {
148161
// 图片信息
149162
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+
}
152167
val date = System.currentTimeMillis() / 1000
153168
put(MediaStore.Images.Media.DATE_ADDED, date)
154169
put(MediaStore.Images.Media.DATE_MODIFIED, date)
@@ -166,7 +181,8 @@ private fun ContentResolver.insertMediaImage(
166181
// 高版本不用查重直接插入,会自动重命名
167182
} else {
168183
// 老版本
169-
val pictures = Environment.getExternalStoragePublicDirectory(ALBUM_DIR)
184+
val pictures =
185+
@Suppress("DEPRECATION") Environment.getExternalStoragePublicDirectory(ALBUM_DIR)
170186
val saveDir = if (relativePath != null) File(pictures, relativePath) else pictures
171187

172188
if (!saveDir.exists() && !saveDir.mkdirs()) {
@@ -192,7 +208,7 @@ private fun ContentResolver.insertMediaImage(
192208
// 保存路径
193209
val imagePath = imageFile.absolutePath
194210
Log.v(TAG, "save file: $imagePath")
195-
put(MediaStore.Images.Media.DATA, imagePath)
211+
put(@Suppress("DEPRECATION") MediaStore.Images.Media.DATA, imagePath)
196212
}
197213
outputFileTaker?.file = imageFile// 回传文件路径,用于设置文件大小
198214
collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
@@ -220,8 +236,8 @@ private fun ContentResolver.queryMediaImage28(imagePath: String): Uri? {
220236
// 查询是否已经存在相同图片
221237
val query = this.query(
222238
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} == ?",
225241
arrayOf(imagePath), null
226242
)
227243
query?.use {

0 commit comments

Comments
 (0)