简体   繁体   中英

How to move a photo/video to an album with Flutter on mobile (iOS and Android)?

Using Flutter on mobile (iOS and Android), how can I move media to an album?

I tried to combine gallery_saver and photo_manager on iOS:

  1. I read a media AssetEntity from Recents
  2. Saved the AssetEntity file under an album with GallerySaver.saveImage . At this point Recents showed the same media twice, makes sense.
  3. Attempted to delete with PhotoManager.editor.deleteWithIds the original AssetEntity 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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM