Skip to content

Commit

Permalink
⚡️ Reorg input streams when saving asset on Android (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Sep 1, 2024
1 parent da1da9c commit 382d114
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 141 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ To know more about breaking changes, see the [Migration Guide][].

## Unreleased

### Breaking changes

`saveImage` now requires `filename` rather than `title` other save methods do not require `title` anymore.

### Improvements

- Allows saving assets with a given orientation value.
- Improves the reading sequence when saving assets, which likely fixes issues with wrong orientation value.
- Reads image size from EXIF rather than decoding from the bitmap factory.
- Upgrades Android EXIF library.

Expand Down
32 changes: 27 additions & 5 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ If you want to see the new feature support, please refer to [readme][] and [chan

<!-- TOC -->
* [Migration Guide](#migration-guide)
* [3.0.x to 3.1](#30x-to-31)
* [3.x to 3.3](#3x-to-33)
* [Overall](#overall)
* [`saveImage`](#saveimage)
* [3.0.x to 3.1](#30x-to-31)
* [Overall](#overall-1)
* [`containsLivePhotos`](#containslivephotos)
* [`AlbumType`](#albumtype)
* [2.x to 3.0](#2x-to-30)
* [Overall](#overall-1)
* [Overall](#overall-2)
* [`AssetEntityImage` and `AssetEntityImageProvider`](#assetentityimage-and-assetentityimageprovider)
* [2.x to 2.8](#2x-to-28)
* [Overall](#overall-2)
* [2.x to 2.2](#2x-to-22)
* [Overall](#overall-3)
* [2.x to 2.2](#2x-to-22)
* [Overall](#overall-4)
* [`assetCount`](#assetcount)
* [1.x to 2.0](#1x-to-20)
* [Overall](#overall-4)
* [Overall](#overall-5)
* [API migrations](#api-migrations)
* [`getAssetListPaged`](#getassetlistpaged)
* [Filtering only videos](#filtering-only-videos)
Expand All @@ -28,6 +31,25 @@ If you want to see the new feature support, please refer to [readme][] and [chan
* [0.5 To 0.6](#05-to-06)
<!-- TOC -->

## 3.x to 3.3

### Overall

In order to let developers write the most precise API usage,
the `title` of `saveImage` has migrated to `filename`.

#### `saveImage`

Before:
```dart
final entity = await PhotoManager.editor.saveImage(bytes, title: 'new.jpg');
```

After:
```dart
final entity = await PhotoManager.editor.saveImage(bytes, filename: 'new.jpg');
```

## 3.0.x to 3.1

### Overall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.fluttercandies.photo_manager.core.utils.IDBUtils
import com.fluttercandies.photo_manager.thumb.ThumbnailUtil
import com.fluttercandies.photo_manager.util.LogUtils
import com.fluttercandies.photo_manager.util.ResultHandler
import java.io.File
import java.util.concurrent.Executors

class PhotoManager(private val context: Context) {
Expand Down Expand Up @@ -167,36 +166,34 @@ class PhotoManager(private val context: Context) {
}

fun saveImage(
image: ByteArray,
bytes: ByteArray,
filename: String,
title: String,
description: String,
relativePath: String,
orientation: Int?
): AssetEntity? {
return dbUtils.saveImage(context, image, title, description, relativePath, orientation)
return dbUtils.saveImage(context, bytes, filename, title, description, relativePath, orientation)
}

fun saveImage(
path: String,
filePath: String,
title: String,
description: String,
relativePath: String,
orientation: Int?
): AssetEntity? {
return dbUtils.saveImage(context, path, title, description, relativePath, orientation)
return dbUtils.saveImage(context, filePath, title, description, relativePath, orientation)
}

fun saveVideo(
path: String,
filePath: String,
title: String,
desc: String,
relativePath: String,
orientation: Int?
): AssetEntity? {
if (!File(path).exists()) {
return null
}
return dbUtils.saveVideo(context, path, title, desc, relativePath, orientation)
return dbUtils.saveVideo(context, filePath, title, desc, relativePath, orientation)
}

fun assetExists(id: String, resultHandler: ResultHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,15 @@ class PhotoManagerPlugin(

Methods.saveImage -> {
try {
val image = call.argument<ByteArray>("image")!!
val bytes = call.argument<ByteArray>("image")!!
val filename = call.argument<String>("filename") ?: ""
val title = call.argument<String>("title") ?: ""
val desc = call.argument<String>("desc") ?: ""
val relativePath = call.argument<String>("relativePath") ?: ""
val orientation = call.argument<Int?>("orientation")
val entity = photoManager.saveImage(
image,
bytes,
filename,
title,
desc,
relativePath,
Expand All @@ -491,13 +493,13 @@ class PhotoManagerPlugin(

Methods.saveImageWithPath -> {
try {
val imagePath = call.argument<String>("path")!!
val filePath = call.argument<String>("path")!!
val title = call.argument<String>("title") ?: ""
val desc = call.argument<String>("desc") ?: ""
val relativePath = call.argument<String>("relativePath") ?: ""
val orientation = call.argument<Int?>("orientation")
val entity = photoManager.saveImage(
imagePath,
filePath,
title,
desc,
relativePath,
Expand All @@ -517,13 +519,13 @@ class PhotoManagerPlugin(

Methods.saveVideo -> {
try {
val videoPath = call.argument<String>("path")!!
val filePath = call.argument<String>("path")!!
val title = call.argument<String>("title")!!
val desc = call.argument<String>("desc") ?: ""
val relativePath = call.argument<String>("relativePath") ?: ""
val orientation = call.argument<Int?>("orientation")
val entity = photoManager.saveVideo(
videoPath,
filePath,
title,
desc,
relativePath,
Expand Down Expand Up @@ -634,4 +636,4 @@ class PhotoManagerPlugin(
val arguments = argument<Map<*, *>>("option")!!
return ConvertUtils.convertToFilterOptions(arguments)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import java.io.File

data class AssetEntity(
val id: Long,
var path: String,
val path: String,
val duration: Long,
val createDt: Long,
val width: Int,
val height: Int,
val type: Int,
val displayName: String,
val modifiedDate: Long,
var orientation: Int,
var lat: Double? = null,
var lng: Double? = null,
val orientation: Int,
val lat: Double? = null,
val lng: Double? = null,
val androidQRelativePath: String? = null,
val mimeType: String? = null
) {
Expand All @@ -26,10 +26,9 @@ data class AssetEntity(
MediaStoreUtils.convertTypeToMediaType(type)
)

val relativePath: String?
get() = if (isAboveAndroidQ) {
androidQRelativePath
} else {
File(path).parent
}
val relativePath: String? = if (isAboveAndroidQ) {
androidQRelativePath
} else {
File(path).parent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,3 @@ fun String.checkDirs() {
targetFile.parentFile!!.mkdirs()
}
}

fun InputStream.getOrientationDegrees(): Int {
return try {
this.use {
return@use ExifInterface(it).rotationDegrees
}
} catch (ignored: Throwable) {
0
}
}
Loading

0 comments on commit 382d114

Please sign in to comment.