Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttkkl committed Aug 30, 2024
1 parent 020b4c2 commit 6319d4e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package io.ssttkkl.mahjongutils.app.utils.image

import android.content.ContentValues
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.toArgb
import io.ssttkkl.mahjongutils.app.MyApp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

actual suspend fun platformWithBackground(
imageBitmap: ImageBitmap,
Expand All @@ -34,4 +41,37 @@ actual suspend fun platformWithBackground(
canvas.drawBitmap(imageBitmap.asAndroidBitmap(), 50f, 50f, null) // 偏移位置50, 50

return newBitmap.asImageBitmap()
}

actual object ImageUtils : CommonImageUtils() {
actual suspend fun save(imageBitmap: ImageBitmap, title: String): Boolean {
return withContext(Dispatchers.IO) {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, title)
put(MediaStore.Images.Media.MIME_TYPE, "image/png")
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
put(MediaStore.Images.Media.IS_PENDING, 1)
}

val uri: Uri = MyApp.current.contentResolver.insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values
) ?: return@withContext false

uri.let {
try {
MyApp.current.contentResolver.openOutputStream(it)?.use {
imageBitmap.asAndroidBitmap().compress(Bitmap.CompressFormat.PNG, 100, it)
} ?: return@let false
values.clear()
values.put(MediaStore.Images.Media.IS_PENDING, 0)
MyApp.current.contentResolver.update(uri, values, null, null)
} catch (e: Exception) {
e.printStackTrace()
return@let false
}
}
return@withContext true
}
}
}

This file was deleted.

0 comments on commit 6319d4e

Please sign in to comment.