[Modify]私聊頁面修改
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.yizhuan.xchat_android_library.common;
|
||||
|
||||
public class Constants {
|
||||
|
||||
//上传的图片 默认大小不能超过大小 640KB
|
||||
public static final int UPLOAD_IMAGE_MAX_FILE_LENGTH = 640;
|
||||
//上传的图片 默认宽高最大值 2340
|
||||
public static final int UPLOAD_IMAGE_MAX_SIZE = 2340;
|
||||
//上传的gif 默认大小不能超过 1MB
|
||||
public static final int UPLOAD_GIF_MAX_SIZE = 1 << 20;
|
||||
|
||||
}
|
@@ -0,0 +1,182 @@
|
||||
package com.yizhuan.xchat_android_library.common.util
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Environment
|
||||
import android.text.TextUtils
|
||||
import android.util.Log
|
||||
import com.yizhuan.xchat_android_library.luban.Luban
|
||||
import com.yizhuan.xchat_android_library.common.file.FileHelper
|
||||
import com.yizhuan.xchat_android_library.common.Constants
|
||||
import com.yizhuan.xchat_android_library.common.application.BaseApp
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
object PhotoCompressUtil {
|
||||
private const val TAG = "PhotoCompressUtil"
|
||||
private const val TAG_IMAGE_COMPRESS = "default"
|
||||
private val photoExtensions = arrayOf("jpg", "png", "jpeg", "bmp", "webp")
|
||||
|
||||
private fun checkIsPhoto(path: String?): Boolean {
|
||||
if (TextUtils.isEmpty(path)) {
|
||||
return false
|
||||
}
|
||||
for (extension in photoExtensions) {
|
||||
if (path!!.endsWith(extension, true)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun compress(context: Context, imgList: MutableList<String>, outPath: String?, callback: PhotosCompressCallback?, leastCompressSize: Int = 200, focusAlpha: Boolean = false, maxSize: Int = Constants.UPLOAD_IMAGE_MAX_SIZE, mostCompressSize: Int = Constants.UPLOAD_IMAGE_MAX_FILE_LENGTH): Job? {
|
||||
val notImgMap = HashMap<Int, String>()
|
||||
val imgs = ArrayList<String>()
|
||||
imgList.forEachIndexed { index, s ->
|
||||
if (checkIsPhoto(s)) {
|
||||
imgs.add(s)
|
||||
} else {
|
||||
notImgMap[index] = s
|
||||
}
|
||||
}
|
||||
if (notImgMap.size == imgList.size) {
|
||||
//纯非图片
|
||||
(imgList as? ArrayList<String>)?.let { callback?.onSuccess(it) }
|
||||
return null
|
||||
} else {
|
||||
return CoroutineScope(Dispatchers.Main + SupervisorJob()).launch {
|
||||
val deferred = async(Dispatchers.IO) {
|
||||
var list: MutableList<File>? = null
|
||||
try {
|
||||
list = Luban.with(context)
|
||||
.load<String>(imgs)
|
||||
.ignoreBy(leastCompressSize)
|
||||
.setTargetDir(outPath)
|
||||
.setFocusAlpha(focusAlpha)
|
||||
.setMaxSize(maxSize)
|
||||
.setMostCompressSize(mostCompressSize)
|
||||
.get()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "compress error: $e")
|
||||
}
|
||||
|
||||
list
|
||||
}
|
||||
val compressedFileList = deferred.await()
|
||||
if (!isActive) return@launch
|
||||
if (compressedFileList.isNullOrEmpty()) {
|
||||
callback?.onFail(Throwable("compress fail"))
|
||||
return@launch
|
||||
}
|
||||
val compressedList = compressedFileList.map { it.path } as? ArrayList
|
||||
|
||||
try {
|
||||
if (notImgMap.isNotEmpty()) {
|
||||
notImgMap.forEach {
|
||||
compressedList?.add(it.key, it.value)
|
||||
}
|
||||
}
|
||||
compressedList?.let { callback?.onSuccess(it) }
|
||||
} catch (e: Exception) {
|
||||
callback?.onFail(Throwable("compress fail"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun compress(context: Context, imgPath: String, outPath: String?, callback: PhotoCompressCallback?, leastCompressSize: Int = 200, focusAlpha: Boolean = false, maxSize: Int = Constants.UPLOAD_IMAGE_MAX_SIZE, mostCompressSize: Int = Constants.UPLOAD_IMAGE_MAX_FILE_LENGTH):Job? {
|
||||
if (!checkIsPhoto(imgPath)) {
|
||||
callback?.onSuccess(imgPath)
|
||||
return null
|
||||
} else {
|
||||
return CoroutineScope(Dispatchers.Main + SupervisorJob()).launch {
|
||||
val deferred = async(Dispatchers.IO) {
|
||||
var list: MutableList<File>? = null
|
||||
try {
|
||||
list = Luban.with(context)
|
||||
.load(imgPath)
|
||||
.ignoreBy(leastCompressSize)
|
||||
.setTargetDir(outPath)
|
||||
.setFocusAlpha(focusAlpha)
|
||||
.setMaxSize(maxSize)
|
||||
.setMostCompressSize(mostCompressSize)
|
||||
.get()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "compress error: $e")
|
||||
}
|
||||
|
||||
list
|
||||
}
|
||||
|
||||
val compressedFileList = deferred.await()
|
||||
if (!isActive) return@launch
|
||||
if (compressedFileList.isNullOrEmpty()) {
|
||||
callback?.onFail(Throwable("compress fail"))
|
||||
} else {
|
||||
callback?.onSuccess(compressedFileList[0].path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步线程-压缩
|
||||
*/
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun synCompress(imgPath: String, outPath: String?, leastCompressSize: Int = 200, focusAlpha: Boolean = false, maxSize: Int = Constants.UPLOAD_IMAGE_MAX_SIZE, mostCompressSize: Int = Constants.UPLOAD_IMAGE_MAX_FILE_LENGTH): String? {
|
||||
return if (!checkIsPhoto(imgPath)) {
|
||||
imgPath
|
||||
} else {
|
||||
var compressedFileList: MutableList<File>? = null
|
||||
try {
|
||||
compressedFileList = Luban.with(BaseApp.getContext())
|
||||
.load(imgPath)
|
||||
.ignoreBy(leastCompressSize)
|
||||
.setTargetDir(outPath)
|
||||
.setFocusAlpha(focusAlpha)
|
||||
.setMaxSize(maxSize)
|
||||
.setMostCompressSize(mostCompressSize)
|
||||
.get()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "compress error: $e")
|
||||
}
|
||||
if (compressedFileList.isNullOrEmpty()) {
|
||||
""
|
||||
} else {
|
||||
compressedFileList[0].path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getCompressCachePath(tag: String = TAG_IMAGE_COMPRESS): String {
|
||||
val path = getCompressLocationPath(tag)
|
||||
FileHelper.ensureDirExists(path)
|
||||
return path
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun clearCompressCache(tag: String = TAG_IMAGE_COMPRESS) {
|
||||
FileHelper.removeAllFile(getCompressLocationPath(tag))
|
||||
}
|
||||
|
||||
private fun getCompressLocationPath(tag: String): String {
|
||||
return "${FileHelper.getRootFilesDir(Environment.DIRECTORY_PICTURES).absolutePath}/compress/${tag}/"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface PhotosCompressCallback {
|
||||
fun onSuccess(compressedImgList: ArrayList<String>)
|
||||
fun onFail(e: Throwable)
|
||||
}
|
||||
|
||||
interface PhotoCompressCallback {
|
||||
fun onSuccess(compressedImg: String)
|
||||
fun onFail(e: Throwable)
|
||||
}
|
Reference in New Issue
Block a user