feat: create delete request
This commit is contained in:
1 parent
03c2a1bbdd
commit
8cb4a4e41b
9 files changed
+390
-42
No files matched your search
@@ -1,7 +1,6 @@
|
||||
# TODO List
|
||||
|
||||
- [ ] 系统相册跟随删除
|
||||
- [ ] 参考:AGC ToolKit Pro,DAZZ
|
||||
- [x] 鸿蒙适配
|
||||
- [ ] 定制水印
|
||||
- [ ] 视频拍摄
|
||||
@@ -9,5 +8,14 @@
|
||||
- [ ] 滤镜面板自动关闭
|
||||
- [ ] 相册导入照片方向处理
|
||||
- [ ] 开发者选项多语言
|
||||
- [ ] raw 格式照片处理
|
||||
- [ ] 对焦
|
||||
- [ ] raw 格式照片处理 / Halide
|
||||
- [ ] 对焦
|
||||
- [ ] 批量导入
|
||||
- [ ] LUT自定义命名
|
||||
|
||||
# 竞品参考
|
||||
|
||||
* AGC ToolKit Pro
|
||||
* DAZZ
|
||||
* varlens
|
||||
* kapi
|
||||
@@ -50,6 +50,11 @@ android {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
|
||||
}
|
||||
}
|
||||
buildFeatures {
|
||||
compose = true
|
||||
}
|
||||
|
||||
@@ -80,6 +80,18 @@ class MainActivity : ComponentActivity() {
|
||||
hasCameraPermission = isGranted
|
||||
}
|
||||
|
||||
private val deletePhotoLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartIntentSenderForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
// 用户确认删除,删除应用内部的照片
|
||||
galleryViewModel.deletePhotoAfterConfirmation()
|
||||
} else {
|
||||
// 用户取消删除
|
||||
galleryViewModel.clearDeleteRequest()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
package com.hinnka.mycamera.gallery
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.media.ThumbnailUtils
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import android.os.Build
|
||||
import android.provider.MediaStore
|
||||
import com.hinnka.mycamera.camera.CaptureInfo
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
@@ -229,16 +232,67 @@ object PhotoManager {
|
||||
?: emptyList()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建删除系统相册照片的请求(弹出确认对话框)
|
||||
*
|
||||
* 仅适用于 Android 11+ (API 30+)
|
||||
* 返回 PendingIntent,需要在 Activity 中通过 startIntentSenderForResult 启动
|
||||
*/
|
||||
fun createDeleteRequest(context: Context, photoId: String): PendingIntent? {
|
||||
return try {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
||||
PLog.w(TAG, "createDeleteRequest requires Android 11+")
|
||||
return null
|
||||
}
|
||||
|
||||
// 加载元数据,获取导出的 URI 列表(使用 runBlocking 同步调用)
|
||||
val metadata = runBlocking {
|
||||
loadMetadata(context, photoId)
|
||||
}
|
||||
val exportedUris = metadata?.exportedUris ?: emptyList()
|
||||
|
||||
if (exportedUris.isEmpty()) {
|
||||
PLog.d(TAG, "No exported URIs to delete for photo: $photoId")
|
||||
return null
|
||||
}
|
||||
|
||||
// 将字符串 URI 转换为 Uri 对象列表
|
||||
val uriList = exportedUris.mapNotNull { uriString ->
|
||||
try {
|
||||
Uri.parse(uriString)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Invalid URI: $uriString", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
if (uriList.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
|
||||
// 创建删除请求(会弹出系统确认对话框)
|
||||
MediaStore.createDeleteRequest(context.contentResolver, uriList)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to create delete request for photo: $photoId", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除照片及其所有相关文件
|
||||
*/
|
||||
suspend fun deletePhoto(context: Context, photoId: String): Boolean {
|
||||
suspend fun deletePhoto(
|
||||
context: Context,
|
||||
photoId: String
|
||||
): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val photoDir = getPhotoDir(context, photoId)
|
||||
if (photoDir.exists()) {
|
||||
photoDir.deleteRecursively()
|
||||
}
|
||||
|
||||
PLog.d(TAG, "Photo deleted: $photoId")
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to delete photo: $photoId", e)
|
||||
@@ -247,6 +301,13 @@ object PhotoManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅删除应用内部的照片,不删除系统相册中的导出照片
|
||||
*/
|
||||
suspend fun deletePhotoOnly(context: Context, photoId: String): Boolean {
|
||||
return deletePhoto(context, photoId)
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载元数据
|
||||
*/
|
||||
|
||||
@@ -17,7 +17,7 @@ import kotlin.text.toInt
|
||||
* 保存 LUT、边框水印、编辑信息和拍摄参数,用于非破坏性编辑和边框水印渲染
|
||||
*/
|
||||
data class PhotoMetadata(
|
||||
val version: Int = 3,
|
||||
val version: Int = 4,
|
||||
// 编辑配置
|
||||
val lutId: String? = null,
|
||||
val lutIntensity: Float = 1f,
|
||||
@@ -39,7 +39,9 @@ data class PhotoMetadata(
|
||||
val focalLength: String? = null,
|
||||
val focalLength35mm: String? = null,
|
||||
val aperture: String? = null,
|
||||
val isImported: Boolean = false
|
||||
val isImported: Boolean = false,
|
||||
// 导出到系统相册的 URI 列表
|
||||
val exportedUris: List<String> = emptyList()
|
||||
) {
|
||||
/**
|
||||
* 分辨率字符串 (用于边框水印显示)
|
||||
@@ -69,6 +71,8 @@ data class PhotoMetadata(
|
||||
put("focalLength35mm", focalLength35mm ?: JSONObject.NULL)
|
||||
put("aperture", aperture ?: JSONObject.NULL)
|
||||
put("isImported", isImported)
|
||||
// 导出的 URI 列表
|
||||
put("exportedUris", org.json.JSONArray(exportedUris))
|
||||
}.toString(2)
|
||||
}
|
||||
|
||||
@@ -78,6 +82,16 @@ data class PhotoMetadata(
|
||||
fun fromJson(json: String): PhotoMetadata? {
|
||||
return try {
|
||||
val obj = JSONObject(json)
|
||||
|
||||
// 解析 exportedUris 列表
|
||||
val exportedUris = mutableListOf<String>()
|
||||
val urisArray = obj.optJSONArray("exportedUris")
|
||||
if (urisArray != null) {
|
||||
for (i in 0 until urisArray.length()) {
|
||||
exportedUris.add(urisArray.getString(i))
|
||||
}
|
||||
}
|
||||
|
||||
PhotoMetadata(
|
||||
version = obj.optInt("version", 1),
|
||||
lutId = if (obj.isNull("lutId")) null else obj.optString("lutId"),
|
||||
@@ -98,7 +112,8 @@ data class PhotoMetadata(
|
||||
focalLength = if (obj.isNull("focalLength")) null else obj.optString("focalLength"),
|
||||
focalLength35mm = if (obj.isNull("focalLength35mm")) null else obj.optString("focalLength35mm"),
|
||||
aperture = if (obj.isNull("aperture")) null else obj.optString("aperture"),
|
||||
isImported = obj.optBoolean("isImported", false)
|
||||
isImported = obj.optBoolean("isImported", false),
|
||||
exportedUris = exportedUris
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to parse JSON", e)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.hinnka.mycamera.ui.gallery
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.IntentSenderRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.fadeIn
|
||||
@@ -64,13 +66,40 @@ fun GalleryScreen(
|
||||
val isSharing by viewModel.isSharing.collectAsState()
|
||||
val isSelectionMode = viewModel.isSelectionMode
|
||||
val selectedPhotos = viewModel.selectedPhotos
|
||||
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.GetContent()
|
||||
) { uri ->
|
||||
uri?.let { viewModel.importPhoto(it) }
|
||||
}
|
||||
|
||||
|
||||
// Activity Result Launcher for batch delete confirmation
|
||||
val batchDeleteLauncher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.StartIntentSenderForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
// User confirmed deletion, delete internal photos
|
||||
viewModel.deleteBatchPhotosAfterConfirmation()
|
||||
} else {
|
||||
// User cancelled deletion
|
||||
viewModel.clearBatchDeleteRequest()
|
||||
}
|
||||
}
|
||||
|
||||
// Monitor batchDeletePendingIntent and launch system delete dialog
|
||||
LaunchedEffect(viewModel.batchDeletePendingIntent) {
|
||||
viewModel.batchDeletePendingIntent?.let { pendingIntent ->
|
||||
try {
|
||||
batchDeleteLauncher.launch(
|
||||
IntentSenderRequest.Builder(pendingIntent.intentSender).build()
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
// Failed to launch, clear the request
|
||||
viewModel.clearBatchDeleteRequest()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
|
||||
// 延迟加载照片列表,避免与跳转动画冲突导致卡顿
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package com.hinnka.mycamera.ui.gallery
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.IntentSenderRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.core.animateFloatAsState
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.gestures.*
|
||||
@@ -56,7 +60,7 @@ fun PhotoDetailScreen(
|
||||
) {
|
||||
val photos by viewModel.photos.collectAsState()
|
||||
val context = LocalContext.current
|
||||
|
||||
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
var showInfoDialog by remember { mutableStateOf(false) }
|
||||
var isZoomed by remember { mutableStateOf(false) }
|
||||
@@ -64,6 +68,37 @@ fun PhotoDetailScreen(
|
||||
var isSaving by remember { mutableStateOf(false) }
|
||||
val isSharing by viewModel.isSharing.collectAsState()
|
||||
|
||||
// Activity Result Launcher for delete confirmation
|
||||
val deletePhotoLauncher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.StartIntentSenderForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
// User confirmed deletion, delete internal photo
|
||||
viewModel.deletePhotoAfterConfirmation { success ->
|
||||
if (success && photos.isEmpty()) {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// User cancelled deletion
|
||||
viewModel.clearDeleteRequest()
|
||||
}
|
||||
}
|
||||
|
||||
// Monitor deletePendingIntent and launch system delete dialog
|
||||
LaunchedEffect(viewModel.deletePendingIntent) {
|
||||
viewModel.deletePendingIntent?.let { pendingIntent ->
|
||||
try {
|
||||
deletePhotoLauncher.launch(
|
||||
IntentSenderRequest.Builder(pendingIntent.intentSender).build()
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
// Failed to launch, clear the request
|
||||
viewModel.clearDeleteRequest()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val pagerState = rememberPagerState(
|
||||
initialPage = initialIndex,
|
||||
pageCount = { photos.size }
|
||||
@@ -242,11 +277,7 @@ fun PhotoDetailScreen(
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
viewModel.deleteCurrentPhoto { success ->
|
||||
if (success && photos.isEmpty()) {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
viewModel.deleteCurrentPhoto()
|
||||
showDeleteDialog = false
|
||||
}
|
||||
) {
|
||||
|
||||
@@ -795,6 +795,17 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
// 使用 98 质量以获得更高的图像清晰度
|
||||
processedBitmap.compress(Bitmap.CompressFormat.JPEG, 98, outputStream)
|
||||
}
|
||||
|
||||
// 保存导出的 URI 到元数据
|
||||
val photoId = PhotoManager.getPhotoIds(context).firstOrNull()
|
||||
if (photoId != null) {
|
||||
val currentMetadata = PhotoManager.loadMetadata(context, photoId) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + it.toString()
|
||||
)
|
||||
PhotoManager.saveMetadata(context, photoId, updatedMetadata)
|
||||
PLog.d(TAG, "Exported URI saved: $it")
|
||||
}
|
||||
}
|
||||
|
||||
processedBitmap.recycle()
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
@@ -123,10 +124,23 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
// 可用的边框列表
|
||||
var availableFrames: List<FrameInfo> by mutableStateOf(emptyList())
|
||||
private set
|
||||
|
||||
|
||||
// 最新照片(用于相机界面显示入口)
|
||||
private val _latestPhoto = MutableStateFlow<PhotoData?>(null)
|
||||
val latestPhoto: StateFlow<PhotoData?> = _latestPhoto.asStateFlow()
|
||||
|
||||
// 待删除的照片(用于 Activity Result 回调)
|
||||
var pendingDeletePhoto: PhotoData? by mutableStateOf(null)
|
||||
private set
|
||||
|
||||
// 删除请求的 PendingIntent(用于启动系统删除对话框)
|
||||
var deletePendingIntent: android.app.PendingIntent? by mutableStateOf(null)
|
||||
private set
|
||||
|
||||
// 批量删除相关状态
|
||||
private var pendingDeletePhotos: List<PhotoData> = emptyList()
|
||||
var batchDeletePendingIntent: android.app.PendingIntent? by mutableStateOf(null)
|
||||
private set
|
||||
|
||||
init {
|
||||
loadPhotos()
|
||||
@@ -407,46 +421,200 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
fun getCurrentPhoto(): PhotoData? {
|
||||
return _photos.value.getOrNull(currentPhotoIndex)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除单张照片
|
||||
* 获取删除系统相册照片的请求 PendingIntent(用于 Android 11+)
|
||||
*
|
||||
* @return PendingIntent 如果有导出的照片需要删除,返回 PendingIntent;否则返回 null
|
||||
*/
|
||||
fun deletePhoto(photo: PhotoData, onComplete: (Boolean) -> Unit = {}) {
|
||||
private fun getDeleteRequest(photo: PhotoData): android.app.PendingIntent? {
|
||||
val context = getApplication<Application>()
|
||||
return PhotoManager.createDeleteRequest(context, photo.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求删除照片
|
||||
*
|
||||
* 这个方法会:
|
||||
* 1. 在 Android 11+ 上检查是否有导出的照片需要删除
|
||||
* 2. 如果有,设置 deletePendingIntent 和 pendingDeletePhoto,UI 层需要监听这些状态并启动删除确认对话框
|
||||
* 3. 如果没有导出的照片,或者在 Android 10 及以下,直接删除照片
|
||||
*/
|
||||
fun requestDeletePhoto(photo: PhotoData) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// Android 11+: 检查是否有导出的照片
|
||||
val pendingIntent = getDeleteRequest(photo)
|
||||
|
||||
if (pendingIntent != null) {
|
||||
// 有导出的照片,需要用户确认
|
||||
pendingDeletePhoto = photo
|
||||
deletePendingIntent = pendingIntent
|
||||
PLog.d(TAG, "Set delete pending intent for photo ${photo.id}")
|
||||
} else {
|
||||
// 没有导出的照片,直接删除应用内照片
|
||||
deletePhotoDirectly(photo)
|
||||
}
|
||||
} else {
|
||||
// Android 10 及以下: 直接删除所有照片
|
||||
deletePhotoDirectly(photo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除删除请求状态
|
||||
*/
|
||||
fun clearDeleteRequest() {
|
||||
pendingDeletePhoto = null
|
||||
deletePendingIntent = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除批量删除请求状态
|
||||
*/
|
||||
fun clearBatchDeleteRequest() {
|
||||
pendingDeletePhotos = emptyList()
|
||||
batchDeletePendingIntent = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接删除照片(不检查系统相册)
|
||||
*/
|
||||
private fun deletePhotoDirectly(photo: PhotoData) {
|
||||
viewModelScope.launch {
|
||||
val success = repository.deletePhoto(photo)
|
||||
if (success) {
|
||||
// PhotoManager.deletePhoto 已由 repository.deletePhoto 调用
|
||||
loadPhotos()
|
||||
PLog.d(TAG, "Photo deleted: ${photo.id}")
|
||||
} else {
|
||||
PLog.e(TAG, "Failed to delete photo: ${photo.id}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅删除应用内部的照片(在用户确认删除系统相册照片后调用)
|
||||
*/
|
||||
fun deletePhotoAfterConfirmation(onComplete: (Boolean) -> Unit = {}) {
|
||||
val photo = pendingDeletePhoto ?: return
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
val success = PhotoManager.deletePhotoOnly(context, photo.id)
|
||||
if (success) {
|
||||
loadPhotos()
|
||||
|
||||
// 如果删除的是当前照片,调整索引
|
||||
if (photo == getCurrentPhoto() && currentPhotoIndex >= _photos.value.size) {
|
||||
currentPhotoIndex = (_photos.value.size - 1).coerceAtLeast(0)
|
||||
}
|
||||
|
||||
PLog.d(TAG, "Photo deleted after confirmation: ${photo.id}")
|
||||
}
|
||||
clearDeleteRequest()
|
||||
onComplete(success)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除当前照片
|
||||
*/
|
||||
fun deleteCurrentPhoto(onComplete: (Boolean) -> Unit = {}) {
|
||||
fun deleteCurrentPhoto() {
|
||||
getCurrentPhoto()?.let { photo ->
|
||||
deletePhoto(photo) { success ->
|
||||
if (success && currentPhotoIndex >= _photos.value.size) {
|
||||
currentPhotoIndex = (_photos.value.size - 1).coerceAtLeast(0)
|
||||
}
|
||||
onComplete(success)
|
||||
}
|
||||
requestDeletePhoto(photo)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量删除选中的照片
|
||||
*
|
||||
* Android 11+: 会使用 MediaStore.createDeleteRequest 弹出系统确认对话框
|
||||
* Android 10-: 直接删除所有照片
|
||||
*/
|
||||
fun deleteSelectedPhotos(onComplete: (Int) -> Unit = {}) {
|
||||
fun deleteSelectedPhotos() {
|
||||
val toDelete = selectedPhotos.toList()
|
||||
if (toDelete.isEmpty()) return
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// Android 11+: 收集所有导出的 URIs
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
val allExportedUris = mutableListOf<Uri>()
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
toDelete.forEach { photo ->
|
||||
val metadata = PhotoManager.loadMetadata(context, photo.id)
|
||||
metadata?.exportedUris?.forEach { uriString ->
|
||||
try {
|
||||
allExportedUris.add(Uri.parse(uriString))
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Invalid URI: $uriString", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allExportedUris.isNotEmpty()) {
|
||||
// 有导出的照片,需要用户确认
|
||||
try {
|
||||
val pendingIntent = MediaStore.createDeleteRequest(
|
||||
context.contentResolver,
|
||||
allExportedUris
|
||||
)
|
||||
pendingDeletePhotos = toDelete
|
||||
batchDeletePendingIntent = pendingIntent
|
||||
PLog.d(TAG, "Set batch delete pending intent for ${toDelete.size} photos")
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to create batch delete request", e)
|
||||
// 创建请求失败,直接删除应用内照片
|
||||
deleteBatchPhotosDirectly(toDelete)
|
||||
}
|
||||
} else {
|
||||
// 没有导出的照片,直接删除应用内照片
|
||||
deleteBatchPhotosDirectly(toDelete)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Android 10 及以下: 直接删除
|
||||
deleteBatchPhotosDirectly(toDelete)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接批量删除照片(不检查系统相册)
|
||||
*/
|
||||
private fun deleteBatchPhotosDirectly(photos: List<PhotoData>) {
|
||||
viewModelScope.launch {
|
||||
val toDelete = selectedPhotos.toList()
|
||||
val deletedCount = repository.deletePhotos(toDelete)
|
||||
|
||||
val deletedCount = repository.deletePhotos(photos)
|
||||
exitSelectionMode()
|
||||
loadPhotos()
|
||||
PLog.d(TAG, "Batch deleted $deletedCount photos")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除确认后的回调(在用户确认删除系统相册照片后调用)
|
||||
*/
|
||||
fun deleteBatchPhotosAfterConfirmation(onComplete: (Int) -> Unit = {}) {
|
||||
val photos = pendingDeletePhotos
|
||||
if (photos.isEmpty()) return
|
||||
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
var deletedCount = 0
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
photos.forEach { photo ->
|
||||
val success = PhotoManager.deletePhotoOnly(context, photo.id)
|
||||
if (success) {
|
||||
deletedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exitSelectionMode()
|
||||
loadPhotos()
|
||||
clearBatchDeleteRequest()
|
||||
onComplete(deletedCount)
|
||||
PLog.d(TAG, "Batch deleted $deletedCount photos after confirmation")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -807,20 +975,20 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val context = getApplication<Application>()
|
||||
|
||||
|
||||
// 读取照片
|
||||
val inputStream = context.contentResolver.openInputStream(photo.uri)
|
||||
val bitmap = BitmapFactory.decodeStream(inputStream)
|
||||
inputStream?.close()
|
||||
|
||||
|
||||
if (bitmap == null) {
|
||||
onComplete(false)
|
||||
return@launch
|
||||
}
|
||||
|
||||
|
||||
// 处理照片
|
||||
val processedBitmap = photoProcessor.process(context, bitmap, metadata, photo.uri)
|
||||
|
||||
|
||||
// 保存到指定目录
|
||||
val filename = "PhotonCamera_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())}$filenameSuffix.jpg"
|
||||
val contentValues = ContentValues().apply {
|
||||
@@ -828,22 +996,30 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
|
||||
put(MediaStore.MediaColumns.RELATIVE_PATH, directory)
|
||||
}
|
||||
|
||||
|
||||
val uri = context.contentResolver.insert(
|
||||
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||||
contentValues
|
||||
)
|
||||
|
||||
|
||||
uri?.let {
|
||||
context.contentResolver.openOutputStream(it)?.use { outputStream ->
|
||||
// 使用 98 质量以获得更高的图像清晰度
|
||||
processedBitmap.compress(Bitmap.CompressFormat.JPEG, 98, outputStream)
|
||||
}
|
||||
|
||||
// 保存导出的 URI 到元数据
|
||||
val currentMetadata = PhotoManager.loadMetadata(context, photo.id) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + it.toString()
|
||||
)
|
||||
PhotoManager.saveMetadata(context, photo.id, updatedMetadata)
|
||||
PLog.d(TAG, "Exported URI saved: $it for photo ${photo.id}")
|
||||
}
|
||||
|
||||
|
||||
processedBitmap.recycle()
|
||||
onComplete(uri != null)
|
||||
|
||||
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to export photo", e)
|
||||
onComplete(false)
|
||||
|
||||
Reference in new issue
Block a user