-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add image save and share actions to media viewer (#857)
* Add image save button * Show media buttons in overlay * Show snackbar on save success, failure
- Loading branch information
Showing
11 changed files
with
418 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/capyreader/app/ui/articles/media/Colors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.capyreader.app.ui.articles.media | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
object MediaColors { | ||
val textColor = Color.White.copy(0.8f) | ||
|
||
val buttonContentColor = Color.White | ||
|
||
val buttonOutlineColor = Color.White | ||
|
||
val buttonContainerColor = Color.Transparent | ||
} |
96 changes: 96 additions & 0 deletions
96
app/src/main/java/com/capyreader/app/ui/articles/media/ImageSaver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.capyreader.app.ui.articles.media | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.net.Uri | ||
import android.os.Environment | ||
import android.provider.MediaStore | ||
import androidx.core.graphics.drawable.toBitmap | ||
import androidx.core.net.toUri | ||
import coil.executeBlocking | ||
import coil.imageLoader | ||
import coil.request.ImageRequest | ||
import com.capyreader.app.common.MD5 | ||
import com.capyreader.app.common.externalImageCacheFile | ||
import com.capyreader.app.common.fileURI | ||
import com.jocmp.capy.logging.CapyLog | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.io.ByteArrayOutputStream | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
|
||
object ImageSaver { | ||
suspend fun saveImage(imageUrl: String, context: Context): Result<Uri> { | ||
return withContext(Dispatchers.IO) { | ||
return@withContext try { | ||
val bitmap = | ||
createBitmap(imageUrl, context) ?: throw IOException("Failed to generate image") | ||
|
||
val contentValues = ContentValues().apply { | ||
put(MediaStore.MediaColumns.DISPLAY_NAME, jpegFileName(imageUrl)) | ||
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg") | ||
put( | ||
MediaStore.MediaColumns.RELATIVE_PATH, | ||
"${Environment.DIRECTORY_PICTURES}/Capy" | ||
) | ||
} | ||
|
||
val uri = context.contentResolver.insert( | ||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | ||
contentValues | ||
) ?: throw IOException("Failed to create MediaStore entry") | ||
|
||
context.contentResolver.openOutputStream(uri)?.use { outputStream -> | ||
outputStream.write(jpegStream(bitmap)) | ||
} ?: throw IOException("Failed to open output stream") | ||
|
||
Result.success(uri) | ||
} catch (e: Exception) { | ||
CapyLog.error("save_img", error = e) | ||
Result.failure(e) | ||
} | ||
} | ||
} | ||
|
||
suspend fun shareImage(imageUrl: String, context: Context): Result<Uri> { | ||
return withContext(Dispatchers.IO) { | ||
return@withContext try { | ||
val bitmap = | ||
createBitmap(imageUrl, context) ?: throw IOException("Failed to generate image") | ||
|
||
val target = context.externalImageCacheFile(jpegFileName(imageUrl)) | ||
|
||
context.contentResolver.openFileDescriptor(target.toUri(), "w")?.use { descriptor -> | ||
FileOutputStream(descriptor.fileDescriptor).use { | ||
it.write(jpegStream(bitmap)) | ||
} | ||
} | ||
|
||
Result.success(context.fileURI(target)) | ||
} catch (e: Exception) { | ||
CapyLog.error("share_img", error = e) | ||
Result.failure(e) | ||
} | ||
} | ||
} | ||
|
||
private fun jpegStream(bitmap: Bitmap): ByteArray { | ||
val byteArrayOutputStream = ByteArrayOutputStream() | ||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream) | ||
return byteArrayOutputStream.toByteArray() | ||
} | ||
|
||
private fun jpegFileName(imageUrl: String): String { | ||
return "${MD5.from(imageUrl)}.jpg" | ||
} | ||
|
||
private fun createBitmap(imageUrl: String, context: Context): Bitmap? { | ||
val imageRequest = ImageRequest.Builder(context) | ||
.data(imageUrl) | ||
.build() | ||
|
||
return context.imageLoader.executeBlocking(imageRequest).drawable?.toBitmap() | ||
} | ||
} |
Oops, something went wrong.