Skip to content

Commit

Permalink
Merge pull request #106 from natsuk4ze/Fix-invalid-file-type
Browse files Browse the repository at this point in the history
Fix auto file conversion
  • Loading branch information
natsuk4ze authored Aug 18, 2023
2 parents 4de2f3a + f209d96 commit 616abaa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ android {
}

dependencies {
implementation 'org.apache.commons:commons-imaging:1.0-alpha3'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:5.0.0'
}
Expand Down
15 changes: 10 additions & 5 deletions android/src/main/java/studio/midoridesign/gal/GalPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import java.io.ByteArrayInputStream;
import java.util.UUID;

import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.Imaging;

public class GalPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware,
PluginRegistry.RequestPermissionsResultListener {
private static final String PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE;
Expand Down Expand Up @@ -124,14 +127,16 @@ private void putMedia(Context context, String path, String album, boolean isImag
if (dotIndex == -1) throw new FileNotFoundException("Extension not found.");

try (InputStream in = new FileInputStream(file)) {
writeData(context, in, isImage, name.substring(dotIndex + 1), album);
writeData(context, in, isImage, name.substring(dotIndex), album);
}
}

private void putMediaBytes(Context context, byte[] bytes, String album)
throws IOException, SecurityException {
ImageFormat imageFormat = Imaging.guessFormat(bytes);
String extension = "." + imageFormat.getDefaultExtension().toLowerCase();
try (InputStream in = new ByteArrayInputStream(bytes)) {
writeData(context, in, true, "jpg", album);
writeData(context, in, true, extension, album);
}
}

Expand All @@ -141,20 +146,20 @@ private void writeData(Context context, InputStream in, boolean isImage, String
ContentValues values = new ContentValues();
String dirPath = isImage || album != null ? Environment.DIRECTORY_PICTURES
: Environment.DIRECTORY_MOVIES;
String name = UUID.randomUUID().toString();

if (USE_EXTERNAL_STORAGE) {
File dir = new File(Environment.getExternalStoragePublicDirectory(dirPath),
album != null ? album : "");
if (!dir.exists()) dir.mkdirs();
String path =
dir.getPath() + File.separator + UUID.randomUUID().toString() + "." + extension;
String path = dir.getPath() + File.separator + name + extension;
values.put(MediaStore.MediaColumns.DATA, path);
} else {
String path = dirPath + (album != null ? File.separator + album : "");
values.put(isImage ? MediaStore.Images.Media.RELATIVE_PATH
: MediaStore.Video.Media.RELATIVE_PATH, path);
}

values.put(MediaStore.MediaColumns.DISPLAY_NAME, name + extension);
Uri uri = resolver.insert(isImage ? IMAGE_URI : VIDEO_URI, values);
try (OutputStream out = resolver.openOutputStream(uri)) {
byte[] buffer = new byte[8192];
Expand Down
7 changes: 3 additions & 4 deletions ios/Classes/GalPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ public class GalPlugin: NSObject, FlutterPlugin {
private func putMedia(
path: String, album: String?, isImage: Bool, completion: @escaping (Bool, Error?) -> Void
) {
let url = URL(fileURLWithPath: path)
writeContent(
assetChangeRequest: {
isImage
? PHAssetChangeRequest.creationRequestForAsset(
from: UIImage(contentsOfFile: path)!)
: PHAssetChangeRequest.creationRequestForAssetFromVideo(
atFileURL: URL(fileURLWithPath: path))!
? PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: url)!
: PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)!
}, album: album, completion: completion
)
}
Expand Down

0 comments on commit 616abaa

Please sign in to comment.