Skip to content

fix: replace hardcoded android storage path with path_provider#1039

Open
vibhutomer wants to merge 1 commit into
mosip:masterfrom
vibhutomer:fix/android-scoped-storage
Open

fix: replace hardcoded android storage path with path_provider#1039
vibhutomer wants to merge 1 commit into
mosip:masterfrom
vibhutomer:fix/android-scoped-storage

Conversation

@vibhutomer
Copy link
Copy Markdown

@vibhutomer vibhutomer commented May 9, 2026

Description

This PR resolves a critical issue where the application crashes or silently fails on modern Android devices (API 29+) due to Scoped Storage restrictions.

Previously, the app attempted to write directly to a hardcoded /storage/emulated/0/Download path. On Android 10 and above, this throws a FileSystemException (Permission Denied) unless the app possesses legacy flags or broad MANAGE_EXTERNAL_STORAGE permissions.

Changes Made

  • Removed the hardcoded /storage/emulated/0/Download path in the FileStorage.getExternalDocumentPath() method.
  • Implemented getExternalStorageDirectory() from the path_provider package to dynamically fetch the correct, OS-approved external storage path.
  • Added a safe fallback to getApplicationDocumentsDirectory() in the event that external storage resolves to null (which can occur on specific custom ROMs or locked enterprise devices).

Related Issue

Closes #1038

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • My changes generate no new warnings or exceptions.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced external storage directory resolution on Android devices to dynamically determine the appropriate storage path instead of relying on a fixed location, improving app reliability and compatibility across different device configurations.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 9, 2026

Warning

Rate limit exceeded

@vibhutomer has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 56 minutes and 44 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0f4b60e7-b60c-4c49-a2e9-b4d749858f9c

📥 Commits

Reviewing files that changed from the base of the PR and between df39ff8 and a006063.

📒 Files selected for processing (1)
  • lib/utils/file_storage.dart

Walkthrough

The FileStorage.getExternalDocumentPath() method now dynamically resolves the Android external storage directory using getExternalStorageDirectory() with a fallback to getApplicationDocumentsDirectory(), replacing the previous hardcoded /storage/emulated/0/Download path. The permission check, directory creation, and return logic remain unchanged.

Changes

Android Scoped Storage Path Resolution

Layer / File(s) Summary
External Storage Path Resolution
lib/utils/file_storage.dart
The Android branch of getExternalDocumentPath() now asynchronously resolves the external storage directory via getExternalStorageDirectory() with a null-safe fallback to getApplicationDocumentsDirectory(), replacing the hardcoded /storage/emulated/0/Download path.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through storage paths,
No hardcoded roads for us!
Android's rules we now obey,
Dynamic paths light up the way,
Safe and sound from API ten and beyond! 🐰📱

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: replacing a hardcoded Android storage path with the path_provider package approach.
Linked Issues check ✅ Passed The PR implementation successfully addresses all key objectives from issue #1038: removes hardcoded path, uses getExternalStorageDirectory(), provides getApplicationDocumentsDirectory() fallback.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the FileStorage.getExternalDocumentPath() method and address the stated objectives; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/utils/file_storage.dart (1)

17-21: ⚡ Quick win

Remove unnecessary Permission.storage request for app-specific external directory.

Since getExternalStorageDirectory() (line 31) returns the app-specific external directory (Android/data/<package>/files), the runtime Permission.storage request at lines 17-21 is unnecessary on Android 10–14. Modern scoped storage allows your app to read/write its own app-specific files without broad storage permissions.

Suggested removal
   static Future<String> getExternalDocumentPath() async {
-    // To check whether permission is given for this app or not.
-    var status = await Permission.storage.status;
-    if (!status.isGranted) {
-      // If not we will ask for permission first
-      await Permission.storage.request();
-    }
     Directory directory = Directory("");
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/utils/file_storage.dart` around lines 17 - 21, Remove the unnecessary
runtime storage permission check/request: delete the Permission.storage.status +
Permission.storage.request() block (references: Permission.storage) since you
are using getExternalStorageDirectory() for app-specific external storage
(getExternalStorageDirectory()). After removal, ensure code still handles
platform differences (no-op on Android 10–14 scoped storage) and remove any
now-unused imports (e.g., permission_handler/Permission) if applicable.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@lib/utils/file_storage.dart`:
- Around line 17-21: Remove the unnecessary runtime storage permission
check/request: delete the Permission.storage.status +
Permission.storage.request() block (references: Permission.storage) since you
are using getExternalStorageDirectory() for app-specific external storage
(getExternalStorageDirectory()). After removal, ensure code still handles
platform differences (no-op on Android 10–14 scoped storage) and remove any
now-unused imports (e.g., permission_handler/Permission) if applicable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bb3537b6-73bd-450a-b3d1-c75ef1ee52d8

📥 Commits

Reviewing files that changed from the base of the PR and between aef4fb6 and df39ff8.

📒 Files selected for processing (1)
  • lib/utils/file_storage.dart

Signed-off-by: vibhutomer <vibhutomer25@gmail.com>
@vibhutomer vibhutomer force-pushed the fix/android-scoped-storage branch from df39ff8 to a006063 Compare May 9, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: App crash on Android 10+ due to hardcoded external storage path in FileStorage

1 participant