Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
alibardide5124 committed Jan 15, 2021
1 parent 17611d8 commit 1f75fdd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
22 changes: 17 additions & 5 deletions app/src/main/java/com/alibardide/imagine/ImageUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.os.Environment
import android.util.DisplayMetrics
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import kotlinx.coroutines.Dispatchers
Expand All @@ -14,20 +15,26 @@ import java.io.*

class ImageUtil(private val listener: ImageListener) {

fun loadImage(context: Context, path: String, imageView: ImageView) {
fun loadImage(context: Context, file: File, imageView: ImageView) {
// Run an AsyncTask
AsyncTaskNeo.executeAsyncTask<Drawable?, Boolean>(
onPreExecute = {},
doInBackground = {
var drawable: Drawable? = null
try {
// Get file from path
val inputStream = withContext(Dispatchers.IO) { FileInputStream(path) }
// Get file as Bitmap
val bitmap = BitmapFactory.decodeFile(file.absolutePath)
// Reduce size to 480
val sizeBase = bitmap.width / 480
val width = bitmap.width / sizeBase
val height = bitmap.height / sizeBase
drawable = RoundedBitmapDrawableFactory.create(context.resources,
Bitmap.createScaledBitmap(bitmap, width, height, true))
// Round the corners
drawable = RoundedBitmapDrawableFactory.create(context.resources, inputStream)
drawable.isCircular = true
drawable.cornerRadius = 20f
} catch (e: IOException) {
e.printStackTrace()
}
drawable
},
Expand All @@ -50,7 +57,7 @@ class ImageUtil(private val listener: ImageListener) {
try {
if (Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()) {
// Get file
val root = context.getExternalFilesDir(null)?.absolutePath + "/$saveDir"
val root = "${getAbsoluteDir(context)}/$saveDir"
val dir = File(root)
if (!dir.exists()) dir.mkdirs()
val sFile = File(dir, "imagine.${file.name}")
Expand Down Expand Up @@ -84,6 +91,11 @@ class ImageUtil(private val listener: ImageListener) {
)
}

private fun getAbsoluteDir(context: Context) : String {
val rootPath = context.getExternalFilesDir(null)!!.absolutePath
val extra = "Android/data/${BuildConfig.APPLICATION_ID}/files"
return File(rootPath.replace(extra, "")).absolutePath
}

}
interface ImageListener {
Expand Down
20 changes: 13 additions & 7 deletions app/src/main/java/com/alibardide/imagine/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.resolution
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.dialog_progress.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -66,14 +68,16 @@ class MainActivity : AppCompatActivity(), ImageListener {
imageFile?.let { file ->
progress.show()
// Launch a GlobalScope
GlobalScope.launch {
GlobalScope.launch(Dispatchers.IO) {
val context = this@MainActivity
// Decode selected file, compress file and save compressed file
val bitmap = BitmapFactory.decodeFile(file.absolutePath)
val compressedImage = Compressor.compress(context, file) {
resolution(bitmap.width, bitmap.height)
}
ImageUtil(context).saveImage(context, compressedImage, saveDir)
withContext(Dispatchers.Main) {
ImageUtil(context).saveImage(context, compressedImage, saveDir)
}
}
}
} else { toast("No image selected").show() }
Expand Down Expand Up @@ -116,11 +120,13 @@ class MainActivity : AppCompatActivity(), ImageListener {
try {
progress.show()
// Load image into a file
imageFile = FileUtil.from(this, data.data!!).also {
ImageUtil(this).loadImage(this, it.path, mainPicture)
hasImage = true
// Make mainCompress button visible
mainCompress.show()
GlobalScope.launch(Dispatchers.IO) {
val context = this@MainActivity
imageFile = FileUtil.from(context, data.data!!).also {
ImageUtil(context).loadImage(context, it, mainPicture)
withContext(Dispatchers.Main) { mainCompress.show() }
hasImage = true
}
}
} catch (e: IOException) { toast("Failed to read picture data!").show() }
}
Expand Down

0 comments on commit 1f75fdd

Please sign in to comment.