How to move a photo/video to an album with Flutter on mobile (iOS and Android)?
April 2020 Update
Thankfully, the author of the library recently updated it to include album removal feature discussed in this answer. You can see their pull request. Or refer to the method invocation removeAssetsInAlbum
in the README
Problem
photo_manager
library is using PHAssetChangeRequest's method deleteAssets(_:). You can see this here. This will delete the actual file rather than just remove it from album (more on this in Explanation section)
Solution
The solution is to use PHAssetCollectionChangeRequest's methods. Depends on your use case:
Moving assets from album to another
Removing assets from an album
Adding assets to an album
Explanation
There's a difference between deleting assets and removing assets. Deleting an asset's file is done via PHAssetChangeRequest which deletes the asset file permanently and its references from albums. But removing an asset is done via PHAssetCollectionChangeRequest which removes the asset's reference (not file) from album/s without deleting the file itself. Check this answer.
What to do? How can this solution be applied?
This section is now obsolete but kept for other use-cases when photo_manager
cannot be used, please refer to the top of this answer if you want to use photo_manager
You have the following options to pick one from that would solve your problem:
- Look for the album methods I explained above in the library
photo_manager
, maybe it's there? EDIT1 I just looked for PHAssetCollectionChangeRequest's methods. Currently not supported/used. - Add an issue to the library and ask the author to make said changes
- Add them to the library yourself via PR or your own cloned version of the library
- Use a platform channel of your own and handle it yourself.
Example Change:
Currently photo_manager
uses PHAssetChangeRequest
's method deleteAssets(_:):
[PHAssetChangeRequest deleteAssets:result];
This should change to using PHAssetCollectionChangeRequest
's method removeAssets(_:):
[PHAssetCollectionChangeRequest removeAssets:result];
Comments
-
thedp over 1 year
Using Flutter on mobile (iOS and Android), how can I move media to an album?
I tried to combine
gallery_saver
andphoto_manager
on iOS:- I read a media
AssetEntity
fromRecents
- Saved the
AssetEntity
file under an album withGallerySaver.saveImage
. At this pointRecents
showed the same media twice, makes sense. - Attempted to delete with
PhotoManager.editor.deleteWithIds
the originalAssetEntity
file, and it showed a native popup asking the user if it's ok to delete the media. Bad user experience...
Is there a way to change
AssetEntity
album, without creating a new file? - I read a media
-
CaiJingLong over 4 yearsYes, this is a problem. I am the author of photo_manager. I will add new APIs in the future to support iOS's scheme of removing assets from albums instead of deleting assets.
-
om-ha over 4 yearsMuch appreciated @K.Cai!
-
CaiJingLong about 4 yearsHI, photo_manager support the function of removing only asset from album instead of deleting asset in 0.5.1-dev.3. The pub release version will be posted few days. @om-ha