Skip to content

Commit

Permalink
Custom Filenames on Android (#57)
Browse files Browse the repository at this point in the history
* Add custom filenames on Android
* Fix bug in `getAlbums()` with trashed items in folder
  • Loading branch information
nkalupahana authored Aug 13, 2023
1 parent 8674eef commit ab1fc80
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Creates an album.
| --------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **`path`** | <code>string</code> | Web URL, base64 encoded URI, or local file path to save. |
| **`albumIdentifier`** | <code>string</code> | Album identifier from getAlbums(). Since 5.0, identifier is used on both Android and iOS. Identifier is required on Android but not on iOS. On iOS 14+, if the identifier is not specified and no permissions have been requested yet, add-only permissions will be requested instead of full permissions (assuming NSPhotoLibraryAddUsageDescription is in Info.plist). |
| **`fileName`** | <code>string</code> | File name to save the image as in the album. Do not include extension. Android only. |


#### MediaAlbumCreate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,23 @@ private void _getAlbums(PluginCall call) {

File albumPath = new File(getAlbumPath());
for (File sub : albumPath.listFiles()) {
if (sub.isDirectory() && sub.listFiles().length == 0) {
JSObject album = new JSObject();
if (sub.isDirectory()) {
// Exclude hidden (trashed) files from count
boolean hasFiles = false;
for (File file : sub.listFiles()) {
if (!file.isHidden()) {
hasFiles = true;
break;
}
}

album.put("name", sub.getName());
album.put("identifier", sub.getAbsolutePath());
albums.put(album);
if (!hasFiles) {
JSObject album = new JSObject();

album.put("name", sub.getName());
album.put("identifier", sub.getAbsolutePath());
albums.put(album);
}
}
}

Expand Down Expand Up @@ -256,7 +267,8 @@ private void _saveMedia(PluginCall call) {
inputPath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri downloadsUri = Uri.parse(inputPath);
File fileInDownloads = new File(downloadsUri.getPath());
inputFile = copyFile(fileInDownloads, getContext().getCacheDir());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
inputFile = copyFile(fileInDownloads, getContext().getCacheDir(), "IMG_" + timeStamp);
fileInDownloads.delete();
cursor.close();
} else {
Expand All @@ -283,7 +295,10 @@ private void _saveMedia(PluginCall call) {
Log.d("ENV LOG - ALBUM DIR", String.valueOf(albumDir));

try {
File expFile = copyFile(inputFile, albumDir);
// generate image file name using current date and time
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
String fileName = call.getString("fileName", "IMG_" + timeStamp);
File expFile = copyFile(inputFile, albumDir, fileName);
scanPhoto(expFile);

JSObject result = new JSObject();
Expand Down Expand Up @@ -319,7 +334,7 @@ private void _createAlbum(PluginCall call) {
}
}

private File copyFile(File inputFile, File albumDir) {
private File copyFile(File inputFile, File albumDir, String fileName) {
// if destination folder does not exist, create it
if (!albumDir.exists()) {
if (!albumDir.mkdir()) {
Expand All @@ -330,9 +345,7 @@ private File copyFile(File inputFile, File albumDir) {
String absolutePath = inputFile.getAbsolutePath();
String extension = absolutePath.substring(absolutePath.lastIndexOf("."));

// generate image file name using current date and time
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
File newFile = new File(albumDir, "IMG_" + timeStamp + extension);
File newFile = new File(albumDir, fileName + extension);

// Read and write image files
FileChannel inChannel = null;
Expand Down
2 changes: 1 addition & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/src/components/SaveMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const SaveMedia = () => {

const savePhotoDataURI = async () => {
setStatus("");
let opts: MediaSaveOptions = { path: photoDataURI, albumIdentifier: await ensureDemoAlbum() };
let opts: MediaSaveOptions = { path: photoDataURI, albumIdentifier: await ensureDemoAlbum(), fileName: "fromDataURI" };
await Media.savePhoto(opts);
setStatus("Saved photo from data URI!");
};
Expand Down
5 changes: 5 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export interface MediaSaveOptions {
* is in Info.plist).
*/
albumIdentifier?: string;
/**
* File name to save the image as in the album.
* Do not include extension. Android only.
*/
fileName?: string;
}

export interface MediaFetchOptions {
Expand Down

0 comments on commit ab1fc80

Please sign in to comment.