92 lines
3.5 KiB
Kotlin
92 lines
3.5 KiB
Kotlin
|
|
package com.aiosman.riderpro.utils
|
||
|
|
|
||
|
|
import android.content.ContentValues
|
||
|
|
import android.content.Context
|
||
|
|
import android.graphics.Bitmap
|
||
|
|
import android.graphics.drawable.BitmapDrawable
|
||
|
|
import android.net.Uri
|
||
|
|
import android.os.Build
|
||
|
|
import android.os.Environment
|
||
|
|
import android.provider.MediaStore
|
||
|
|
import android.widget.Toast
|
||
|
|
import coil.ImageLoader
|
||
|
|
import coil.request.ImageRequest
|
||
|
|
import coil.request.SuccessResult
|
||
|
|
import com.aiosman.riderpro.utils.Utils.getImageLoader
|
||
|
|
import kotlinx.coroutines.Dispatchers
|
||
|
|
import kotlinx.coroutines.withContext
|
||
|
|
import java.io.FileNotFoundException
|
||
|
|
import java.io.OutputStream
|
||
|
|
|
||
|
|
object File {
|
||
|
|
suspend fun saveImageToGallery(context: Context, url: String) {
|
||
|
|
val loader = getImageLoader(context)
|
||
|
|
|
||
|
|
val request = ImageRequest.Builder(context)
|
||
|
|
.data(url)
|
||
|
|
.allowHardware(false) // Disable hardware bitmaps.
|
||
|
|
.build()
|
||
|
|
|
||
|
|
val result = (loader.execute(request) as SuccessResult).drawable
|
||
|
|
val bitmap = (result as BitmapDrawable).bitmap
|
||
|
|
|
||
|
|
val contentValues = ContentValues().apply {
|
||
|
|
put(MediaStore.Images.Media.DISPLAY_NAME, "image_${System.currentTimeMillis()}.jpg")
|
||
|
|
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
|
||
|
|
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
|
||
|
|
}
|
||
|
|
|
||
|
|
val uri = context.contentResolver.insert(
|
||
|
|
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||
|
|
contentValues
|
||
|
|
)
|
||
|
|
uri?.let {
|
||
|
|
val outputStream: OutputStream? = context.contentResolver.openOutputStream(it)
|
||
|
|
outputStream.use { stream ->
|
||
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream!!)
|
||
|
|
}
|
||
|
|
withContext(Dispatchers.Main) {
|
||
|
|
Toast.makeText(context, "Image saved to gallery", Toast.LENGTH_SHORT).show()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fun saveImageToMediaStore(context: Context, displayName: String, bitmap: Bitmap): Uri? {
|
||
|
|
val imageCollections = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||
|
|
} else {
|
||
|
|
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
||
|
|
}
|
||
|
|
|
||
|
|
val imageDetails = ContentValues().apply {
|
||
|
|
put(MediaStore.Images.Media.DISPLAY_NAME, displayName)
|
||
|
|
put(MediaStore.Images.Media.MIME_TYPE, "image/jpg")
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
put(MediaStore.Images.Media.IS_PENDING, 1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
val resolver = context.applicationContext.contentResolver
|
||
|
|
val imageContentUri = resolver.insert(imageCollections, imageDetails) ?: return null
|
||
|
|
|
||
|
|
return try {
|
||
|
|
resolver.openOutputStream(imageContentUri, "w").use { os ->
|
||
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os!!)
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||
|
|
imageDetails.clear()
|
||
|
|
imageDetails.put(MediaStore.Images.Media.IS_PENDING, 0)
|
||
|
|
resolver.update(imageContentUri, imageDetails, null, null)
|
||
|
|
}
|
||
|
|
|
||
|
|
imageContentUri
|
||
|
|
} catch (e: FileNotFoundException) {
|
||
|
|
// Some legacy devices won't create directory for the Uri if dir not exist, resulting in
|
||
|
|
// a FileNotFoundException. To resolve this issue, we should use the File API to save the
|
||
|
|
// image, which allows us to create the directory ourselves.
|
||
|
|
null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|