Skip to content

Commit

Permalink
Take picture: Change filename template to usual camera format, by @gs…
Browse files Browse the repository at this point in the history
…antner, #1655

New example: IMG_20220414-014807.jpg
  • Loading branch information
gsantner committed Apr 14, 2022
1 parent 0db38d5 commit 7146fb6
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions app/src/main/java/net/gsantner/opoc/util/ShareUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*#######################################################
*
* Maintained by Gregor Santner, 2017-
* Developed/Maintained by Gregor Santner, 2017-2022
* https://gsantner.net/
*
* License of this file: Apache 2.0 (Commercial upon request)
* https://www.apache.org/licenses/LICENSE-2.0
* https://github.com/gsantner/opoc/#licensing
* License of this file: Creative Commons Zero (CC0 1.0) / Public Domain
*
#########################################################*/
package net.gsantner.opoc.util;
Expand Down Expand Up @@ -85,8 +83,8 @@
@SuppressWarnings({"UnusedReturnValue", "WeakerAccess", "SameParameterValue", "unused", "deprecation", "ConstantConditions", "ObsoleteSdkInt", "SpellCheckingInspection", "JavadocReference", "ConstantLocale"})
public class ShareUtil {
public final static String EXTRA_FILEPATH = "real_file_path_2";
public final static SimpleDateFormat SDF_RFC3339_ISH = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss", Locale.getDefault());
public final static SimpleDateFormat SDF_IMAGES = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()); //20190511-230845
public final static SimpleDateFormat DATEFORMAT_RFC3339ISH = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss", Locale.getDefault());
public final static SimpleDateFormat DATEFORMAT_IMG = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()); //20190511-230845
public final static String MIME_TEXT_PLAIN = "text/plain";
public final static String PREF_KEY__SAF_TREE_URI = "pref_key__saf_tree_uri";

Expand Down Expand Up @@ -392,7 +390,7 @@ public static String getFilenameWithTimestamp(String... A0prefixA1postfixA2ext)
final String prefix = (((A0prefixA1postfixA2ext != null && A0prefixA1postfixA2ext.length > 0 && !TextUtils.isEmpty(A0prefixA1postfixA2ext[0])) ? A0prefixA1postfixA2ext[0] : "Screenshot") + "_").trim().replaceFirst("^_$", "");
final String postfix = ("_" + ((A0prefixA1postfixA2ext != null && A0prefixA1postfixA2ext.length > 1 && !TextUtils.isEmpty(A0prefixA1postfixA2ext[1])) ? A0prefixA1postfixA2ext[1] : "")).trim().replaceFirst("^_$", "");
final String ext = (A0prefixA1postfixA2ext != null && A0prefixA1postfixA2ext.length > 2 && !TextUtils.isEmpty(A0prefixA1postfixA2ext[2])) ? A0prefixA1postfixA2ext[2] : "jpg";
return String.format("%s%s%s.%s", prefix.trim(), SDF_IMAGES.format(new Date()), postfix.trim(), ext.toLowerCase().replace(".", "").replace("jpeg", "jpg"));
return String.format("%s%s%s.%s", prefix.trim(), DATEFORMAT_IMG.format(new Date()), postfix.trim(), ext.toLowerCase().replace(".", "").replace("jpeg", "jpg"));
}

/**
Expand Down Expand Up @@ -706,21 +704,21 @@ public String requestCameraPicture(final File target) {
if (!(_context instanceof Activity)) {
throw new RuntimeException("Error: ShareUtil.requestCameraPicture needs an Activity Context.");
}
final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final String timestampedFilename = getFilenameWithTimestamp("IMG", "", "jpg");
final File storageDir = target != null ? target : new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");

String cameraPictureFilepath = null;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(_context.getPackageManager()) != null) {
File photoFile;
try {
// Create an image file name
if (target != null && !target.isDirectory()) {
photoFile = target;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss", Locale.ENGLISH);
File storageDir = target != null ? target : new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
String imageFileName = ((new ContextUtils(_context).rstr("app_name")).replaceAll("[^a-zA-Z0-9\\.\\-]", "_") + "_").replace("__", "_") + sdf.format(new Date());
photoFile = new File(storageDir, imageFileName + ".jpg");
photoFile = new File(storageDir, timestampedFilename);
if (!photoFile.getParentFile().exists() && !photoFile.getParentFile().mkdirs()) {
photoFile = File.createTempFile(imageFileName + "_", ".jpg", storageDir);
photoFile = File.createTempFile(timestampedFilename.replace(".jpg", "_"), ".jpg", storageDir);
}
}

Expand All @@ -736,8 +734,7 @@ public String requestCameraPicture(final File target) {
// Continue only if the File was successfully created
if (photoFile != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(_context, getFileProviderAuthority(), photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(_context, getFileProviderAuthority(), photoFile));
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
Expand Down

0 comments on commit 7146fb6

Please sign in to comment.