android - 缓存文件可以用于容量分配吗?(使用位图压缩)
问题描述
通过Android策略,我们不是直接访问文件路径,而是希望使用cache和fileprovider在缓存文件中找到文件,减小文件大小,发送到服务器。
您是否知道压缩缓存文件的正确方法?有没有更好的办法?使用的数据库是 Google Firebase Storage。
哦,我使用缓存文件的原因是参考cameraX拍摄的照片文件。
这是我的代码。
@Throws(IOException::class)
fun compressImageWithFilePath(context: Context, filePath: String?, targetMB: Int) {
var image : Bitmap
var exifInterface : ExifInterface? = null
val fileOutputStream : FileOutputStream
val file : File
var externalFile = File(context.externalCacheDir.toString())
var selectedFile : File? = null
var files = externalFile.listFiles()
for (i in files.indices) {
ILog.iLogDebug(TAG, "filePath : ${filePath},${i} : ${files[i].name}")
if (filePath != null) {
if(filePath.contains(files[i].name!!)) {
ILog.iLogDebug(TAG, "일치")
selectedFile = files[i]
}
}
}
if(selectedFile != null) {
ILog.iLogDebug(TAG, selectedFile.path)
//var file = FileProvider.getUriForFile(context, "kr.gowoonwoori.oniontime.fileprovider", selectedFile!!)
exifInterface = ExifInterface(selectedFile.absolutePath)
image = BitmapFactory.decodeFile(selectedFile.absolutePath)
file = selectedFile
fileOutputStream = FileOutputStream(selectedFile.path)
} else {
exifInterface = ExifInterface(filePath!!)
image = BitmapFactory.decodeFile(filePath)
file = File(filePath)
fileOutputStream = FileOutputStream(filePath)
}
val exifOrientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL
)
val exifDegree = exifOrientationToDegrees(exifOrientation)
image = rotate(image, exifDegree)
try {
val length = file.length()
val fileSizeInKB = length / 1024.0
val fileSizeInMB = fileSizeInKB / 1024.0
var quality = 100
if (fileSizeInMB > targetMB) {
quality = (targetMB / fileSizeInMB * 100).toInt()
}
image.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream)
fileOutputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
val uploadTask: UploadTask = when (type) {
CloudStorageFileType.FILE -> {
//storageReference.putFile(Uri.fromFile(File(filePath)))
var externalFile = File(context.externalCacheDir.toString())
var selectedFile : File? = null
var files = externalFile.listFiles()
for (i in files.indices) {
ILog.iLogDebug(TAG, "filePath : ${filePath},${i} : ${files[i].name}")
if (filePath != null) {
if(filePath.contains(files[i].name!!)) {
ILog.iLogDebug(TAG, "일치")
selectedFile = files[i]
}
}
}
if(selectedFile != null) {
var file = FileProvider.getUriForFile(context, "kr.gowoonwoori.oniontime.fileprovider", selectedFile!!)
ILog.iLogDebug(TAG, file.path)
storageReference.putFile(Uri.fromFile(File(selectedFile.path)))
} else {
storageReference.putFile(Uri.fromFile(File(filePath)))
}
}
解决方案
推荐阅读
- regex - 使用正则表达式将字符串转换为 JSON
- c# - 从 C# 重命名网页
- javascript - 具有 useContext() 的子项未使用当前值重新呈现
- python - 匹配所有内容,直到括号内有数字
- delphi - 如何检测窃取特定快捷方式的未知对象?
- java - 当 EditText 为空白并按下按钮时,Android 应用程序崩溃
- javascript - 在 React TypeScript 中键入输入的 keyup
- wcf - 无法在 ASP.NET Core 5.0 下的 IIS 中托管 CoreWcf
- angular - 我想将日期转换为 dd-mm-yyyy
- python - 尝试从 gstreamer appsink 获取 numpy 图像导致缓冲区太小错误