From 50d3b798fe9e56e48acb4745cbf00d02864d18c8 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 19 Aug 2022 14:07:12 +0800 Subject: [PATCH] [Android] Ignore the existence when saving images with path (#815) --- CHANGELOG.md | 6 +++ .../core/utils/AndroidQDBUtils.kt | 4 +- .../photo_manager/core/utils/DBUtils.kt | 15 +++---- .../photo_manager/core/utils/IDBUtils.kt | 39 +++---------------- example/pubspec.yaml | 4 +- pubspec.yaml | 2 +- 6 files changed, 25 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 423e28ce..95d038b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ that can be found in the LICENSE file. --> # CHANGELOG +## 2.2.1 + +### Fixes + +- Fix saving images with path on Android 29-. (#815) + ## 2.2.0 ### Breaking changes diff --git a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/AndroidQDBUtils.kt b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/AndroidQDBUtils.kt index a3a64a66..8b7b4848 100644 --- a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/AndroidQDBUtils.kt +++ b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/AndroidQDBUtils.kt @@ -222,7 +222,7 @@ object AndroidQDBUtils : IDBUtils { private fun assetKeys() = IDBUtils.storeImageKeys + IDBUtils.storeVideoKeys + IDBUtils.typeKeys + arrayOf(MediaStore.MediaColumns.RELATIVE_PATH) - override fun getAssetEntity(context: Context, id: String): AssetEntity? { + override fun getAssetEntity(context: Context, id: String, checkIfExists: Boolean): AssetEntity? { val keys = assetKeys().distinct().toTypedArray() val selection = "${MediaStore.MediaColumns._ID} = ?" val args = arrayOf(id) @@ -234,7 +234,7 @@ object AndroidQDBUtils : IDBUtils { null ) ?: return null cursor.use { - return if (it.moveToNext()) it.toAssetEntity(context) + return if (it.moveToNext()) it.toAssetEntity(context, checkIfExists) else null } } diff --git a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/DBUtils.kt b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/DBUtils.kt index d9a5e56e..973ab420 100644 --- a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/DBUtils.kt +++ b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/DBUtils.kt @@ -221,7 +221,7 @@ object DBUtils : IDBUtils { return list } - override fun getAssetEntity(context: Context, id: String): AssetEntity? { + override fun getAssetEntity(context: Context, id: String, checkIfExists: Boolean): AssetEntity? { val keys = (IDBUtils.storeImageKeys + IDBUtils.storeVideoKeys + locationKeys + IDBUtils.typeKeys).distinct().toTypedArray() val selection = "${MediaStore.MediaColumns._ID} = ?" @@ -236,7 +236,7 @@ object DBUtils : IDBUtils { ) ?: return null cursor.use { return if (it.moveToNext()) { - it.toAssetEntity(context) + it.toAssetEntity(context, checkIfExists) } else { null } @@ -329,13 +329,13 @@ object DBUtils : IDBUtils { null, null ) ?: return null - cursor.use {cursor -> - if (cursor.moveToNext()) { - val targetPath = cursor.getString(0) + cursor.use { + if (it.moveToNext()) { + val targetPath = it.getString(0) targetPath.checkDirs() val outputStream = FileOutputStream(targetPath) refreshInputStream() - outputStream.use { os -> inputStream.use { it.copyTo(os) } } + outputStream.use { os -> inputStream.use { iStream -> iStream.copyTo(os) } } } } val id = ContentUris.parseId(insertUri) @@ -404,7 +404,8 @@ object DBUtils : IDBUtils { val contentUri = cr.insert(uri, values) ?: return null val id = ContentUris.parseId(contentUri) - val assetEntity = getAssetEntity(context, id.toString()) + // Ignore the existence of the file, it'll be exported later. + val assetEntity = getAssetEntity(context, id.toString(), checkIfExists = false) if (!savePath) { val tmpPath = assetEntity?.path!! tmpPath.checkDirs() diff --git a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/IDBUtils.kt b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/IDBUtils.kt index 992e779b..d17753c0 100644 --- a/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/IDBUtils.kt +++ b/android/src/main/kotlin/com/fluttercandies/photo_manager/core/utils/IDBUtils.kt @@ -106,7 +106,7 @@ interface IDBUtils { option: FilterOption ): List - fun getAssetEntity(context: Context, id: String): AssetEntity? + fun getAssetEntity(context: Context, id: String, checkIfExists: Boolean = true): AssetEntity? fun getMediaType(type: Int): Int { return when (type) { @@ -146,13 +146,13 @@ interface IDBUtils { return getLong(getColumnIndex(columnName)) } - fun Cursor.getDouble(columnName: String): Double { - return getDouble(getColumnIndex(columnName)) - } +// fun Cursor.getDouble(columnName: String): Double { +// return getDouble(getColumnIndex(columnName)) +// } - fun Cursor.toAssetEntity(context: Context): AssetEntity? { + fun Cursor.toAssetEntity(context: Context, checkIfExists: Boolean = true): AssetEntity? { val path = getString(MediaStore.MediaColumns.DATA) - if (path.isNotBlank() && !File(path).exists()) { + if (checkIfExists && path.isNotBlank() && !File(path).exists()) { return null } @@ -419,33 +419,6 @@ interface IDBUtils { return uri } - fun getUriFromMediaType(id: String, mediaType: Int, isOrigin: Boolean = false): Uri { - var uri = when (mediaType) { - MEDIA_TYPE_IMAGE -> Uri.withAppendedPath( - MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - id - ) - MEDIA_TYPE_VIDEO -> Uri.withAppendedPath( - MediaStore.Video.Media.EXTERNAL_CONTENT_URI, - id - ) - MEDIA_TYPE_AUDIO -> Uri.withAppendedPath( - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - id - ) - MEDIA_TYPE_PLAYLIST -> Uri.withAppendedPath( - MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, - id - ) - else -> return Uri.EMPTY - } - - if (isOrigin) { - uri = MediaStore.setRequireOriginal(uri) - } - return uri - } - fun throwMsg(msg: String): Nothing { throw RuntimeException(msg) } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 552e46d8..f4b4418c 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,6 +1,6 @@ name: photo_manager_example description: Demonstrates how to use the photo_manager plugin. -version: 2.2.0+13 +version: 2.2.1+14 publish_to: none environment: @@ -12,7 +12,7 @@ dependencies: sdk: flutter http: ^0.13.0 - oktoast: ^3.1.3+1 + oktoast: ^3.2.0 path_provider: ^2.0.5 photo_manager: path: ../ diff --git a/pubspec.yaml b/pubspec.yaml index e5417466..df039e6d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: photo_manager description: A Flutter plugin that provides assets abstraction management APIs on Android, iOS, and macOS. repository: https://github.com/fluttercandies/flutter_photo_manager -version: 2.2.0 +version: 2.2.1 environment: sdk: ">=2.13.0 <3.0.0"