Anki-Android icon indicating copy to clipboard operation
Anki-Android copied to clipboard

Issue get external files dir

Open Nishthajain7 opened this issue 1 month ago • 4 comments

**

Purpose / Description

The app incorrectly showed a fatal error when initializeAnkiDroidDirectory() fails to create a new directory, even if a previously valid collection directory already exists. Add logic to check if an existing AnkiDroid directory is valid before showing the fatal error.

Fixes

  • Fixes #19551

Approach

A fatal error is shown only if both:

  • No existing valid directory is available, and
  • Creating or accessing a new directory fails.

How Has This Been Tested?

  • Ran unit tests in AnkiDroidAppTest.kt

Learning (optional, can help others)

Describe the research stage

  • Executed libanki module tests

Checklist

Please, go through these checks before submitting the PR.

  • [x] You have a descriptive commit message with a short title (first line, max 50 chars).
  • [x] You have commented your code, particularly in hard-to-understand areas
  • [x] You have performed a self-review of your own code
  • [x] UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • [x] UI Changes: You have tested your change using the Google Accessibility Scanner

Nishthajain7 avatar Dec 09 '25 09:12 Nishthajain7

The app incorrectly showed a fatal error when initializeAnkiDroidDirectory() fails to create a new directory, even if a previously valid collection directory already exists.

Could you submit an issue or walk me through the logic here?

david-allison avatar Dec 09 '25 10:12 david-allison

Ok, I'm at a computer, could you walk me through the logic. As I see it:

  • A collection directory exists, but files cannot be written to it

  • The fallback directory is unusable due to a fatal storage error

    • A user can neither write files to the current directory, nor restore to a valid directory
    • => Show a fatal error
  • https://github.com/ankidroid/Anki-Android/commit/6c247c68dc69cee819894534d690d43a8456f34c

  • https://github.com/ankidroid/Anki-Android/pull/19661

https://github.com/ankidroid/Anki-Android/blob/6c247c68dc69cee819894534d690d43a8456f34c/AnkiDroid/src/test/java/com/ichi2/anki/DeckPickerNoExternalFilesDirTest.kt#L66-L75

david-allison avatar Dec 09 '25 10:12 david-allison

This issue is a different case:

  • The existing collection directory is valid and writable.
  • But getExternalFilesDir() returns null temporarily.
  • The code treats this fallback failure as a fatal error without first checking the existing valid directory. So the app shows a fatal error even though it already has a usable path.

Nishthajain7 avatar Dec 09 '25 11:12 Nishthajain7

  • The existing collection directory is valid and writable. => getCurrentAnkiDroidDirectory returns a valid value
  • The existing collection directory is valid and writable. => initializeAnkiDroidDirectory does not fail, and the method exits

https://github.com/ankidroid/Anki-Android/blob/6c247c68dc69cee819894534d690d43a8456f34c/AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidApp.kt#L279-L311

Given the above and this logic, I don't see how getExternalFilesDir would be called, the method should complete as expected

david-allison avatar Dec 09 '25 11:12 david-allison