Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Keep file name in Android #115

Merged
merged 9 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions android/src/main/java/studio/midoridesign/gal/GalPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.util.UUID;

import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.Imaging;
Expand Down Expand Up @@ -68,9 +67,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
case "putImage": {
new Thread(() -> {
try {
putMedia(pluginBinding.getApplicationContext(), call.argument("path"),
call.argument("album"), call.method.contains("Image"));

putMedia(call.argument("path"), call.argument("album"),
call.method.contains("Image"));
new Handler(Looper.getMainLooper()).post(() -> result.success(null));
} catch (Exception e) {
handleError(e, result);
Expand All @@ -81,9 +79,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
case "putImageBytes": {
new Thread(() -> {
try {
putMediaBytes(pluginBinding.getApplicationContext(), call.argument("bytes"),
call.argument("album"));

putMediaBytes(call.argument("bytes"), call.argument("album"));
new Handler(Looper.getMainLooper()).post(() -> result.success(null));
} catch (Exception e) {
handleError(e, result);
Expand Down Expand Up @@ -119,59 +115,67 @@ public void run() {
}
}

private void putMedia(Context context, String path, String album, boolean isImage)
private void putMedia(String path, String album, boolean isImage)
throws IOException, SecurityException, FileNotFoundException {
File file = new File(path);
String name = file.getName();
int dotIndex = name.lastIndexOf('.');
if (dotIndex == -1) throw new FileNotFoundException("Extension not found.");

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

private void putMediaBytes(Context context, byte[] bytes, String album)
throws IOException, SecurityException {
private void putMediaBytes(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, extension, album);
writeData(in, true, "image", extension, album);
}
}

private void writeData(Context context, InputStream in, boolean isImage, String extension,
private void writeData(InputStream in, boolean isImage, String name, String extension,
String album) throws IOException, SecurityException, FileNotFoundException {
ContentResolver resolver = context.getContentResolver();
ContentResolver resolver = pluginBinding.getApplicationContext().getContentResolver();
ContentValues values = createContentValues(isImage, name, extension, album);

Uri uri = resolver.insert(isImage ? IMAGE_URI : VIDEO_URI, values);
try (OutputStream out = resolver.openOutputStream(uri)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}

@SuppressWarnings("EmptyStatementCheck")
private ContentValues createContentValues(boolean isImage, String name, String extension,
String album) {
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 + name + extension;
String path;
String n = dir.getPath() + File.separator + name;
for (int i = 0; new File(path = n + (i == 0 ? "" : i) + extension).exists(); i++);

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];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
return values;
}

private void open() {
Context context = pluginBinding.getApplicationContext();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT <= 23) {
Expand All @@ -181,7 +185,7 @@ private void open() {
intent.setData(IMAGE_URI);
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
pluginBinding.getApplicationContext().startActivity(intent);
}

private boolean hasAccess() {
Expand Down
8 changes: 8 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="SuppressWarningsFilter" />
<module name="TreeWalker">
<module name="SuppressWarningsHolder" />
</module>
</module>