Skip to content

Conversation

@7se7en72025
Copy link
Contributor

@7se7en72025 7se7en72025 commented Nov 23, 2025

fix_waringi_addfile100mb

Purpose / Description

AnkiWeb enforces a 100MiB limit on media files. Currently, AnkiDroid allows users to add files larger than this limit, which leads to sync failures and potential OutOfMemoryErrors later on. This PR implements a warning system to alert users when they attempt to add a file exceeding this limit.

Fixes

Approach

  1. Core Logic: Added ANKIWEB_MAX_MEDIA_FILE_SIZE constant (100 MiB) to Media.kt and implemented a size check in Media.addFile(). This ensures all entry points for adding media are covered.
  2. Exception Handling: Media.addFile() throws an IllegalArgumentException with a specific message format (MEDIA_SIZE_LIMIT_EXCEEDED:...) when the limit is breached.
  3. UI Feedback: NoteEditorFragment catches this exception and displays an AlertDialog warning the user.
  4. User Choice: The dialog offers "Cancel" (default) or "Add Anyway" (bypasses the check) options, giving users control while ensuring they are informed.

How Has This Been Tested?

Tested manually on an Android emulator (Pixel 4 API 30) and physical device:

  1. Small File: Added an image < 100 MiB -> Added successfully without warning.
  2. Large File: Added a video file > 100 MiB -> Warning dialog appeared correctly.
  3. Cancel Action: Clicked "Cancel" in dialog -> File was not added.
  4. Add Anyway Action: Clicked "Add Anyway" -> File was added successfully despite the warning.

Checklist

Please, go through these checks before submitting the PR.

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

@github-actions
Copy link
Contributor

Important

Maintainers: This PR contains Strings changes

  1. Sync Translations before merging this PR and wait for the action to complete
  2. Review and merge the auto-generated PR in order to sync all user-submitted translations
  3. Sync Translations again and merge the PR so the huge automated string changes caused by merging this PR are by themselves and easy to review

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

Thanks!!

As I said an hour ago

It's best if we reach a quick consensus on this one before continuing

Drafting until then

* Files exceeding this limit cannot be synced to AnkiWeb.
* @see <a href="https://faqs.ankiweb.net/are-there-limits-on-file-sizes-on-ankiweb.html">AnkiWeb FAQ</a>
*/
const val ANKIWEB_MAX_MEDIA_FILE_SIZE = 100 * 1024 * 1024L // 100 MiB = 104,857,600 bytes
Copy link
Member

Choose a reason for hiding this comment

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

This will need exposing in Anki-Android-Backend

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@7se7en72025 7se7en72025 Nov 26, 2025

Choose a reason for hiding this comment

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

Should I keep the constant here or wait for #609? ?
aslo Happy to work on ankidroid/Anki-Android-Backend#609 if needed.

} catch (e: IllegalArgumentException) {
// Check if this is a media size limit exception
val message = e.message ?: ""
if (message.startsWith("MEDIA_SIZE_LIMIT_EXCEEDED:")) {
Copy link
Member

Choose a reason for hiding this comment

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

make a different exception type if you're doing this.

val message = e.message ?: ""
if (message.startsWith("MEDIA_SIZE_LIMIT_EXCEEDED:")) {
val parts = message.substringAfter("MEDIA_SIZE_LIMIT_EXCEEDED: ").split("|")
if (parts.size == 3) {
Copy link
Member

Choose a reason for hiding this comment

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

This is really risky, store the data you need in an exception

// backend text ends with dot
val successMessage = TR.importingCardsAdded(count).replace(".", "")
showSnackbar(successMessage, Snackbar.LENGTH_SHORT)
showSnackbar(successMessage)
Copy link
Member

Choose a reason for hiding this comment

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

what's the reasoning here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverting this

@david-allison david-allison marked this pull request as draft November 23, 2025 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn users on attaching media files which breach the AnkiWeb limits (100MB+)

2 participants