diff --git a/lib/src/main/java/com/sonsation/image_compressor/Compression.kt b/lib/src/main/java/com/sonsation/image_compressor/Compression.kt index 8615f50..a9797b2 100644 --- a/lib/src/main/java/com/sonsation/image_compressor/Compression.kt +++ b/lib/src/main/java/com/sonsation/image_compressor/Compression.kt @@ -24,11 +24,11 @@ class Compression(val context: Context) { val constraints by lazy { mutableListOf() } - 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() diff --git a/lib/src/main/java/com/sonsation/image_compressor/ImageCompressor.kt b/lib/src/main/java/com/sonsation/image_compressor/ImageCompressor.kt index d1f0ade..fccdaa4 100644 --- a/lib/src/main/java/com/sonsation/image_compressor/ImageCompressor.kt +++ b/lib/src/main/java/com/sonsation/image_compressor/ImageCompressor.kt @@ -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 @@ -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() } } \ No newline at end of file diff --git a/lib/src/main/java/com/sonsation/image_compressor/utils/ImageUtil.kt b/lib/src/main/java/com/sonsation/image_compressor/utils/ImageUtil.kt index 361dd50..6ea652b 100644 --- a/lib/src/main/java/com/sonsation/image_compressor/utils/ImageUtil.kt +++ b/lib/src/main/java/com/sonsation/image_compressor/utils/ImageUtil.kt @@ -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()}") @@ -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()}") @@ -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")