Skip to content

Commit

Permalink
Add setImageName
Browse files Browse the repository at this point in the history
  • Loading branch information
sonsation committed Sep 24, 2024
1 parent 22091ca commit a830d1c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
15 changes: 10 additions & 5 deletions lib/src/main/java/com/sonsation/image_compressor/Compression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.sonsation.image_compressor.utils.ImageUtil.toSampledBitmap
import com.sonsation.image_compressor.utils.ImageUtil.use
import com.sonsation.image_compressor.utils.ImageUtil.write
import java.io.File
import java.util.UUID

class Compression(val context: Context) {

Expand All @@ -24,7 +25,11 @@ class Compression(val context: Context) {
val constraints by lazy {
mutableListOf<Constraint>()
}
var saveDirectory: String = "${context.cacheDir.absolutePath}/compressed_images"
var imageName = ""
get() = field.ifEmpty {
UUID.randomUUID().toString().replace("-", "")
}
var saveDirectory = "${context.cacheDir.absolutePath}/compressed_images"

var input: Any? = null

Expand Down Expand Up @@ -78,10 +83,10 @@ class Compression(val context: Context) {
input ?: throw IllegalArgumentException("No image source provided")

return when (input) {
is String -> File((input as String)).saveToDir(saveDirectory)
is File -> (input as File).saveToDir(saveDirectory)
is Bitmap -> (input as Bitmap).saveToDir(saveDirectory)
is Uri -> (input as Uri).saveToDir(context, saveDirectory)
is String -> File((input as String)).saveToDir(imageName, saveDirectory)
is File -> (input as File).saveToDir(imageName, saveDirectory)
is Bitmap -> (input as Bitmap).saveToDir(imageName, saveDirectory)
is Uri -> (input as Uri).saveToDir(context, imageName, saveDirectory)
else -> throw IllegalArgumentException("Unsupported input type")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class ImageCompressor(private val context: Context) {
return this
}

fun setImageName(imageName: String): ImageCompressor {
compression.imageName = imageName
return this
}

fun setScale(@Scale scale: Float): ImageCompressor {
compression.scale = scale
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object ImageUtil {
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}

internal fun File.saveToDir(path: String): File {
internal fun File.saveToDir(imageName: String, path: String): File {

val dirFile = File(path)

Expand All @@ -38,12 +38,12 @@ object ImageUtil {
throw IllegalArgumentException("path is file")
}

val destFile = File(path, "${System.currentTimeMillis()}")
val destFile = File(path, imageName)

return this.copyTo(destFile, true)
}

internal fun Bitmap.saveToDir(path: String): File {
internal fun Bitmap.saveToDir(imageName: String, path: String): File {

val dirFile = File(path)

Expand All @@ -55,7 +55,7 @@ object ImageUtil {
throw IllegalArgumentException("path is file")
}

val file = File(path, "${System.currentTimeMillis()}")
val file = File(path, imageName)
val outputStream = file.outputStream()

outputStream.use {
Expand All @@ -66,7 +66,7 @@ object ImageUtil {
return file
}

internal fun Uri.saveToDir(context: Context, path: String): File {
internal fun Uri.saveToDir(context: Context, imageName: String, path: String): File {

val dirFile = File(path)

Expand All @@ -79,7 +79,7 @@ object ImageUtil {
}

val inputStream = context.contentResolver.openInputStream(this) ?: throw NullPointerException("inputStream is null")
val file = File(path, "${System.currentTimeMillis()}")
val file = File(path, imageName)

BufferedOutputStream(file.outputStream(), BUFFER_SIZE).use { output ->
inputStream.use { input ->
Expand Down

0 comments on commit a830d1c

Please sign in to comment.