Android DCIM相册保存
生活随笔
收集整理的這篇文章主要介紹了
Android DCIM相册保存
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、工具類
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/*** 相冊保存操作*Time:2022/3/22 10:09*/ object DCIMUtil {/*** 根據(jù) Android Q 區(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}/*** 復(fù)制文件** @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}}/*** 插入相冊 部分機型適配(區(qū)分手機系統(tǒng)版本 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}}}二、代碼調(diào)用
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()}}}總結(jié)
以上是生活随笔為你收集整理的Android DCIM相册保存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java常问面试题
- 下一篇: 测试:如何选择合适的正交表