Skip to content

Commit

Permalink
[Android] Ignore the existence when saving images with path (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Aug 19, 2022
1 parent f596d29 commit 50d3b79
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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} = ?"
Expand All @@ -236,7 +236,7 @@ object DBUtils : IDBUtils {
) ?: return null
cursor.use {
return if (it.moveToNext()) {
it.toAssetEntity(context)
it.toAssetEntity(context, checkIfExists)
} else {
null
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface IDBUtils {
option: FilterOption
): List<AssetEntity>

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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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: ../
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 50d3b79

Please sign in to comment.