首页 > 解决方案 > 缓存文件可以用于容量分配吗?(使用位图压缩)

问题描述

通过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)))
                }
            }

标签: androidfirebasebitmap

解决方案


推荐阅读