Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sonsation committed Sep 12, 2024
1 parent 4c9b36c commit a9c5533
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class Compression(val context: Context) {
val constraints by lazy {
mutableListOf<Constraint>()
}
var saveDirectory: String = context.cacheDir.absolutePath
var saveDirectory: String = "${context.cacheDir.absolutePath}/compressed_images"

var input: Any? = null

fun getCompressedImage(): File {
fun compressImage(): File {

val imageFile = getInputFile()

Expand Down
26 changes: 14 additions & 12 deletions lib/src/main/java/com/sonsation/image_compressor/ImageCompressor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ class ImageCompressor(private val context: Context) {
Compression(context)
}

fun setSaveDirectory(path: String): ImageCompressor {
compression.saveDirectory = path
return this
}

fun setSaveDirectory(file: File): ImageCompressor {
compression.saveDirectory = file.absolutePath
return this
}

fun setImage(path: String): ImageCompressor {
compression.input = path
return this
Expand Down Expand Up @@ -79,10 +69,22 @@ class ImageCompressor(private val context: Context) {
}

fun getBitmapImage(): Bitmap {
return compression.getCompressedImage().toBitmap()
return compression.compressImage().use {
it.toBitmap()
}
}

fun getFile(): File {
return compression.getCompressedImage()
return compression.compressImage()
}

fun save(path: String) {
compression.saveDirectory = path
compression.compressImage()
}

fun save(file: File) {
compression.saveDirectory = file.absolutePath
compression.compressImage()
}
}
24 changes: 12 additions & 12 deletions lib/src/main/java/com/sonsation/image_compressor/utils/ImageUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ object ImageUtil {

val dirFile = File(path)

if (dirFile.isFile) {
throw IllegalArgumentException("path is file")
if (!dirFile.exists()) {
dirFile.mkdirs()
}

if (dirFile.exists()) {
dirFile.mkdirs()
if (dirFile.isFile) {
throw IllegalArgumentException("path is file")
}

val destFile = File(path, "${System.currentTimeMillis()}")
Expand All @@ -47,12 +47,12 @@ object ImageUtil {

val dirFile = File(path)

if (dirFile.isFile) {
throw IllegalArgumentException("path is file")
if (!dirFile.exists()) {
dirFile.mkdirs()
}

if (dirFile.exists()) {
dirFile.mkdirs()
if (dirFile.isFile) {
throw IllegalArgumentException("path is file")
}

val file = File(path, "${System.currentTimeMillis()}")
Expand All @@ -70,12 +70,12 @@ object ImageUtil {

val dirFile = File(path)

if (dirFile.isFile) {
throw IllegalArgumentException("path is file")
if (!dirFile.exists()) {
dirFile.mkdirs()
}

if (dirFile.exists()) {
dirFile.mkdirs()
if (dirFile.isFile) {
throw IllegalArgumentException("path is file")
}

val inputStream = context.contentResolver.openInputStream(this) ?: throw NullPointerException("inputStream is null")
Expand Down

0 comments on commit a9c5533

Please sign in to comment.