Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for uri #150

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compressor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ apply plugin: 'kotlin-android'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'jacoco-android'

group = 'com.github.shrastiRisor'

buildscript {
repositories {
mavenCentral()
Expand Down
17 changes: 17 additions & 0 deletions compressor/src/main/java/id/zelory/compressor/Compressor.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package id.zelory.compressor

import android.content.Context
import android.net.Uri
import id.zelory.compressor.constraint.Compression
import id.zelory.compressor.constraint.default
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -30,4 +31,20 @@ object Compressor {
}
return@withContext result
}

suspend fun compress(
context: Context,
imageFileUri: Uri,
coroutineContext: CoroutineContext = Dispatchers.IO,
compressionPatch: Compression.() -> Unit = { default() }
) = withContext(coroutineContext) {
val compression = Compression().apply(compressionPatch)
var result = copyToCache(context, imageFileUri)
compression.constraints.forEach { constraint ->
while (constraint.isSatisfied(result).not()) {
result = constraint.satisfy(result)
}
}
return@withContext result
}
}
57 changes: 57 additions & 0 deletions compressor/src/main/java/id/zelory/compressor/Util.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package id.zelory.compressor

import android.content.Context
import android.database.Cursor
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.ExifInterface
import android.net.Uri
import android.os.ParcelFileDescriptor
import android.provider.OpenableColumns
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
* Created on : January 24, 2020
Expand Down Expand Up @@ -82,6 +90,55 @@ internal fun copyToCache(context: Context, imageFile: File): File {
return imageFile.copyTo(File("${cachePath(context)}${imageFile.name}"), true)
}

fun copyToCache(context: Context, srcFileUri: Uri): File {
val cacheFile = File("${cachePath(context)}${getFileName(context, srcFileUri)}")
cacheFile.parentFile.mkdirs()
if (cacheFile.exists()) {
cacheFile.delete()
}
cacheFile.createNewFile()
cacheFile.deleteOnExit()
val fd = context.contentResolver.openFileDescriptor(srcFileUri, "r")
val inputStream = ParcelFileDescriptor.AutoCloseInputStream(fd)
val outputStream = FileOutputStream(cacheFile)
inputStream.use {
outputStream.use {
inputStream.copyTo(outputStream)
}
}
return cacheFile
}

fun getFileName(context: Context, uri: Uri) : String {
val resolver = context.contentResolver
val cursor = resolver.query(
uri, arrayOf(OpenableColumns.DISPLAY_NAME
), null, null, null
)
cursor.use {
val nameIndex = it!!.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (it.moveToFirst()) {
return it.getString(nameIndex)
} else {
val prefix = "IMG_" + SimpleDateFormat(
"yyyyMMdd_",
Locale.getDefault()
).format(Date()) + System.nanoTime()
return when (val fileMimeType = resolver.getType(uri)) {
"image/jpg", "image/jpeg" -> {
"$prefix.jpeg"
}
"image/png" -> {
"$prefix.png"
}
else -> {
throw IllegalStateException("$fileMimeType fallback display name not supported")
}
}
}
}
}

fun overWrite(imageFile: File, bitmap: Bitmap, format: Bitmap.CompressFormat = imageFile.compressFormat(), quality: Int = 100): File {
val result = if (format == imageFile.compressFormat()) {
imageFile
Expand Down