Skip to content

Commit 47c3348

Browse files
author
Ahmed Elsayed
committed
fix(mobile-resources-native): add downloaded file to media collection in android
1 parent b6ad6bd commit 47c3348

File tree

1 file changed

+19
-3
lines changed
  • packages/jsActions/mobile-resources-native/src/file-download

1 file changed

+19
-3
lines changed

packages/jsActions/mobile-resources-native/src/file-download/DownloadFile.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77
// Other code you write will be lost the next time you deploy the project.
88
import { Platform } from "react-native";
9-
import RNBlobUtil from "react-native-blob-util";
9+
import RNBlobUtil, { Mediatype } from "react-native-blob-util";
1010
import FileViewer from "react-native-file-viewer";
11+
import mimeTypes from "mime";
12+
1113
// BEGIN EXTRA CODE
1214
function formatPath(...pathArgs: string[]): string {
1315
return pathArgs.filter(arg => !!arg).join("/");
@@ -52,14 +54,28 @@ export async function DownloadFile(file: mendix.lib.MxObject, openWithOS: boolea
5254
const sanitizedFileName = sanitizeFileName(fileName);
5355
const baseDir = Platform.OS === "ios" ? dirs.DocumentDir : dirs.DownloadDir;
5456
const filePath = mx.data.getDocumentUrl(file.getGuid(), Number(file.get("changedDate")));
55-
const accessiblePath = await getUniqueFilePath(baseDir, sanitizedFileName);
57+
let accessiblePath;
5658

5759
if (Platform.OS === "ios") {
60+
accessiblePath = await getUniqueFilePath(baseDir, sanitizedFileName);
5861
await RNBlobUtil.fs.cp(filePath, accessiblePath);
5962
} else {
63+
const tempPath = await getUniqueFilePath(baseDir, sanitizedFileName);
6064
const base64Data = await mx.readFileBlob(filePath);
6165
const base64Content = base64Data?.split(",")[1];
62-
await RNBlobUtil.fs.createFile(accessiblePath, base64Content, "base64");
66+
await RNBlobUtil.fs.createFile(tempPath, base64Content, "base64");
67+
const mimeType = mimeTypes.getType(fileName) as Mediatype;
68+
accessiblePath = await RNBlobUtil.MediaCollection.copyToMediaStore(
69+
{
70+
name: sanitizedFileName,
71+
mimeType: mimeType ?? "*",
72+
parentFolder: ""
73+
},
74+
"Download",
75+
tempPath
76+
);
77+
78+
RNBlobUtil.fs.unlink(tempPath);
6379
}
6480
if (openWithOS) {
6581
await FileViewer.open(accessiblePath, {

0 commit comments

Comments
 (0)