Skip to content

Get incorrect Address ("fs.dirs.DownloadDir") to save File in android #445

@amirnasr94

Description

@amirnasr94

I wrote a function and I want to save an Excel file that I fetch via an API to the file manager of the Android device.
According to the documentation I am using fs.dirs.DownloadDir to get the path like /storage/emulated/0/Download
but unfortunately the path I am getting is wrong //storage/emulated/0/Android/data/com.Rahava/files/Download.
How can I solve this problem?
When I hardcode the address /storage/emulated/0/Download everything is fine. But I am looking for a better way.

`
export async function downloadFile(reportId: string) {

if (typeof reportId !== "string") return;
try {
const { fs, android } = ReactNativeBlobUtil;
const response = await getExcelFile(reportId);

if (response?.StatusCode !== 200) {
  errorToast("متاسفانه مشکلی رخ داده است. دوباره امتحان کنید!");
  return;
}

const fileName = response?.FileName;
const downloadPath = fs.dirs.DownloadDir + "/" + fileName;
await fs.writeFile(downloadPath, response?.FileBytes as string, "base64");
await fs.scanFile([
  {
    path: downloadPath,
    mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  },
]);

if (Platform.OS === "android") {
  android.addCompleteDownload({
    title: fileName,
    description: "Excel-Data has been downloaded.",
    mime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    path: downloadPath,
    showNotification: true,
  });
}

successToast("فایل با موفقیت دانلود شد.");

} catch (error) {
console.log(error);
errorToast("متاسفانه دانلود فایل با مشکل مواجه شد. دوباره تلاش کنید.");
return null;
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions