flutter_photo_manager icon indicating copy to clipboard operation
flutter_photo_manager copied to clipboard

Fix MP4 videos saving result in 3GP on Android API <=28

Open JamieYee opened this issue 1 year ago • 15 comments

Fixed the issue that the video format changes to 3gp when saving mp4 videos to the album on Android 9

JamieYee avatar Sep 18 '24 10:09 JamieYee

Could you provide a test case in which saving 3gp will fail? Your changes don't tell the idea and why it will fix the issue well.

AlexV525 avatar Sep 18 '24 10:09 AlexV525

pubspec.yaml

photo_manager: ^3.3.0 photo_manager_image_provider: ^2.1.1

image image

use my code

e0bd912a05e01fcca51f557d9eb0d26

device:

Redmi Note5 android 9 MI 8 Lite android 9 MEIZU Note9 android 9

JamieYee avatar Sep 19 '24 06:09 JamieYee

It is still unclear why the PR fixes the issue. Could you provide a minimal reproducible example?

AlexV525 avatar Sep 19 '24 10:09 AlexV525

example code

var appCacheDir = await getApplicationCacheDirectory();
String filePath = '${appCacheDir.path}/aaa.mp4';
File result = File(filePath);
var entity = await PhotoManager.editor.saveVideo(
      result,
     title: path.basename(result.path),
     );  
print('assetEntity===${entity!.title}===${await entity.file}');

On Android 9, the source file is mp4 and converted to 3gp format through saveVideo

JamieYee avatar Sep 20 '24 04:09 JamieYee

Is this happening on all videos or just one specific video? Could you file an issue to summarize all the details? And provide a minimal runnable example so we can run to reproduce.

AlexV525 avatar Sep 20 '24 06:09 AlexV525

All videos on android9

demo: https://github.com/yishangfei/test1

image

https://github.com/user-attachments/assets/64cb697d-a3a2-4453-8726-b0e84bf29ba8

JamieYee avatar Sep 21 '24 02:09 JamieYee

I added it in line 455 according to your latest code.

JamieYee avatar Oct 15 '24 07:10 JamieYee

Still working on this?

AlexV525 avatar Oct 24 '24 02:10 AlexV525

now there is no problem. I only modified the version below Android 10. It’s just that the timestamp needs to be used when saving. If the original name is used a second time, the entity returned by PhotoManager.editor.saveVideo will be null.

JamieYee avatar Oct 25 '24 02:10 JamieYee

If the original name is used a second time, the entity returned by PhotoManager.editor.saveVideo will be null.

We need to figure out why.

Also please do another rebase on the latest changes and remove unnecessary changes.

AlexV525 avatar Oct 25 '24 03:10 AlexV525

The automatic renaming function (for example, video (1).mp4) is implemented in Android 11 and above. In earlier versions, if you repeat cr.insert in the insertUri method, an exception will be thrown. "Cannot insert the new asset."

 val uri = cr.insert(contentUri, values) ?: throw RuntimeException("Cannot insert the new asset.")

Just like the pictures I provided before,versions below Android Q use timestamp naming to insert, or write a renaming logic that is the same as that of higher versions.

JamieYee avatar Oct 26 '24 06:10 JamieYee

The automatic renaming function (for example, video (1).mp4) is implemented in Android 11 and above.

TIL. Are there any docs referencing this?

EDIT: Could you also write it down as comment in the code?

AlexV525 avatar Oct 26 '24 06:10 AlexV525

I have not been able to find any official documentation. I found the solution on stackoverflow. ContentResolver.insert always returning null

add the following comment to the code. Is it OK?

When using the ContentResolver.insert() method to insert a file with the same name, an exception will indeed be thrown if the file with the same name already exists. This is because in Android 10 and below, MediaStore does not have the automatic renaming function.

JamieYee avatar Oct 26 '24 06:10 JamieYee

add the following comment to the code. Is it OK?

How about:

Using a duplicate filename that already exists on the device will cause the insertion to fail on Android API 30-.

AlexV525 avatar Oct 26 '24 07:10 AlexV525

add the following comment to the code. Is it OK?

How about:

Using a duplicate filename that already exists on the device will cause the insertion to fail on Android API 30-.

i submitted

JamieYee avatar Oct 26 '24 09:10 JamieYee

I've also added some format changes and fixed the nullable saving result.

AlexV525 avatar Oct 30 '24 09:10 AlexV525