作者:雅枝建彰3 | 来源:互联网 | 2023-10-10 23:59
一、工具类packagecom.szsh.zpb_company.utilsimportandroid.content.ContentValuesimportandroid.co
一、工具类
package com.szsh.zpb_company.utilsimport android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.text.TextUtils
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream/*** 相册保存操作*Author: YFS(893145181@qq.com)*Time:2022/3/22 10:09*/
object DCIMUtil {/*** 根据 Android Q 区分地址* @param context* @return*/fun getDCIMPath(context: Context): String {var fileName = ""fileName = if (Build.VERSION.SDK_INT >= 29) {context.getExternalFilesDir("")?.absolutePath.toString() + "/current/"} else {when {"Xiaomi".equals(Build.BRAND, ignoreCase = true) -> { // 小米手机Environment.getExternalStorageDirectory().path.toString() + "/DCIM/Camera/"}"HUAWEI".equals(Build.BRAND, ignoreCase = true) -> {Environment.getExternalStorageDirectory().path.toString() + "/DCIM/Camera/"}"HONOR".equals(Build.BRAND, ignoreCase = true) -> {Environment.getExternalStorageDirectory().path.toString() + "/DCIM/Camera/"}"OPPO".equals(Build.BRAND, ignoreCase = true) -> {Environment.getExternalStorageDirectory().path.toString() + "/DCIM/Camera/"}"vivo".equals(Build.BRAND, ignoreCase = true) -> {Environment.getExternalStorageDirectory().path.toString() + "/DCIM/Camera/"}"samsung".equals(Build.BRAND, ignoreCase = true) -> {Environment.getExternalStorageDirectory().path.toString() + "/DCIM/Camera/"}else -> {Environment.getExternalStorageDirectory().path.toString() + "/DCIM/"}}}val file = File(fileName)return if (file.mkdirs()) {fileName} else fileName}/*** 判断android Q (10 ) 版本* @return*/private fun isAndroidQ(): Boolean {return Build.VERSION.SDK_INT >= 29}/*** 复制文件** @param oldPathName* @param newPathName* @return*/fun copyFile(oldPathName: String?, newPathName: String?): Boolean {return try {val oldFile = File(oldPathName)if (!oldFile.exists()) {return false} else if (!oldFile.isFile) {return false} else if (!oldFile.canRead()) {return false}val fileInputStream = FileInputStream(oldPathName)val fileOutputStream = FileOutputStream(newPathName)val buffer = ByteArray(1024)var byteRead: Intwhile (-1 != fileInputStream.read(buffer).also { byteRead = it }) {fileOutputStream.write(buffer, 0, byteRead)}fileInputStream.close()fileOutputStream.flush()fileOutputStream.close()true} catch (e: Exception) {e.printStackTrace()false}}/*** 插入相册 部分机型适配(区分手机系统版本 Android Q)* @param context* @param filePath* @return*/fun insertMediaPic(context: Context, filePath: String?): Boolean {if (TextUtils.isEmpty(filePath)) return falseval file = File(filePath)//判断android Q (10 ) 版本return if (isAndroidQ()) {if (!file.exists()) {false} else {try {MediaStore.Images.Media.insertImage(context.contentResolver,file.absolutePath,file.name,null)true} catch (e: java.lang.Exception) {e.printStackTrace()false}}} else {val values = ContentValues()values.put(MediaStore.Images.Media.DATA, file.absolutePath)values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")values.put(MediaStore.Images.ImageColumns.DATE_TAKEN,System.currentTimeMillis().toString() + "")context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://" + file.absolutePath)))true}}}
二、代码调用
mVb.btnSave.setOnClickListener {showDialog(context)GlobalScope.launch(Dispatchers.IO) {val file = Glide.with(context).asFile().load(url).submit().get()LogUtils.e("Glide path:$file")//文件夹位置val parentPath = DCIMUtil.getDCIMPath(context)//文件名位置val fileName = System.currentTimeMillis().toString()+".png"val filePath = parentPath+fileNameDCIMUtil.copyFile(file.path,filePath)val isSave = DCIMUtil.insertMediaPic(context,filePath)withContext(Dispatchers.Main) {if (isSave) {ToastUtils.showCenter("图片已保存到相册")}else{ToastUtils.showCenter("图片保存失败")}hideDialog()dismiss()}}}