Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to media files through AnkiDroid API #17062

Open
piotrbrzezina opened this issue Sep 13, 2024 · 6 comments
Open

Access to media files through AnkiDroid API #17062

piotrbrzezina opened this issue Sep 13, 2024 · 6 comments

Comments

@piotrbrzezina
Copy link

Hi, I'm trying to write an app that will launch upon unlocking the phone, and its only task will be to present one card, then close after receiving a response. Unfortunately, I'm struggling with one problem, namely how to display images and play audio files because I don't have access to them due to the new Android permissions. I'm wondering if there's a way around this?

I was thinking about whether it would be possible to add another method to the AnkiDroid API that, return an image/audio binary data if it had access to such a file. Do you think this is doable in Android?

Copy link

welcome bot commented Sep 13, 2024

Hello! 👋 Thanks for logging this issue. Please remember we are all volunteers here, so some patience may be required before we can get to the issue. Also remember that the fastest way to get resolution on an issue is to propose a change directly, https://github.com/ankidroid/Anki-Android/wiki/Contributing

@mikehardy
Copy link
Member

Hey there - that seems feasible. We already have a ContentResolver - it allows you to fetch a card IIRC, it could also allow you to fetch an array (or whatever ContentResolver calls it - IIRC it is kind of a database-ish design so maybe a "result set" or whatever) of all media related to that card

I imagine you'd want file name, media object size then the data stream or similar

Anything that did something like that seems reasonable to me

@piotrbrzezina
Copy link
Author

Thanks @mikehardy for the reply, but unfortunately, I'm not sure if I understood it correctly (I'm not an Android developer, I've only learned a bit about it). Are you saying that I can already retrieve image data using the AnkiDroid API, or did you mean that a new method needs to be added to the API, which would return file data for a specific card?

@mikehardy
Copy link
Member

@piotrbrzezina I should have looked more closely before, and I'm sorry I was ambiguous.

It appears that media files are on the ReviewInfo object, which sounds like it might be just what you need:

* JSONArray | MEDIA_FILES | read-only | The media files, like images and sound files, contained in the cards.

@piotrbrzezina
Copy link
Author

piotrbrzezina commented Sep 28, 2024

Thanks for the information. I was trying to do what you proposed, but in my case, it is not working. In that JSON, I only get an empty array '[]'. Could you check if I'm doing it correctly? This is the code I used to get data from the JSON array.

          val deckSelector = "limit=?,deckID=?"
          val deckArguments = arrayOfNulls<String>(2)
          deckArguments[0] = "" + 1
          deckArguments[1] = "" + deckId
          
          val card = HashMap<String, String>()
          val cardCursor = context.contentResolver.query(
                  FlashCardsContract.ReviewInfo.CONTENT_URI,
                  null,
                  deckSelector,
                  deckArguments,
                  null
          )
          
          if (cardCursor?.moveToFirst() == true) {
              val noteIdIndex = cardCursor.getColumnIndex(FlashCardsContract.ReviewInfo.NOTE_ID)
              val cardIdIndex = cardCursor.getColumnIndex(FlashCardsContract.ReviewInfo.CARD_ORD)
              val buttonCountIndex = cardCursor.getColumnIndex(FlashCardsContract.ReviewInfo.BUTTON_COUNT)
              val nextReviewIndex = cardCursor.getColumnIndex(FlashCardsContract.ReviewInfo.NEXT_REVIEW_TIMES)
              val mediaFilesIndex = cardCursor.getColumnIndex(FlashCardsContract.ReviewInfo.MEDIA_FILES)
          
              if (noteIdIndex > -1 && cardIdIndex > -1) {
                  val noteId = cardCursor.getString(noteIdIndex)
                  val specificCardId = cardCursor.getString(cardIdIndex)
                  val buttonCount = cardCursor.getString(buttonCountIndex)
                  val reviewIndex = cardCursor.getString(nextReviewIndex)
                  val mediaFiles = cardCursor.getString(mediaFilesIndex)

Maybe the issue is that the deck I'm trying to work with is not following some guidelines?

@mikehardy
Copy link
Member

Sorry - I honestly don't know, this may be a case of needing to get AnkiDroid building locally and adding debug code into the part of AnkiDroid that populates (or should populate) the media files, and perhaps doing the same with the api that gets built into your app. I apologize but I won't have time to pursue this myself, so getting a locally test/debug environment for the other side of the API seems like fastest path forward

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

No branches or pull requests

2 participants