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

Keep the original image file name when it's attached #9116

Closed
BrayanDSO opened this issue Jun 17, 2021 · 7 comments · Fixed by #10329
Closed

Keep the original image file name when it's attached #9116

BrayanDSO opened this issue Jun 17, 2021 · 7 comments · Fixed by #10329
Milestone

Comments

@BrayanDSO
Copy link
Member

BrayanDSO commented Jun 17, 2021

Is your feature request related to a problem? Please describe.
Sometimes I want to use the same image in multiple cards. If I know the image file name, I can just type <img src="imageFileName.extension">.

The problem: When I attach an image with AnkiDroid, it changes the image file name to something like "img9843278434234908", so it's difficult to use my image again.

Describe the solution you'd like
AnkiDroid keeping the original file name, like the Anki Desktop version does.

Additional context
I believe it's somewhat related to #5192

@welcome
Copy link

welcome bot commented Jun 17, 2021

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

@Akshay0701
Copy link
Member

Akshay0701 commented Jun 22, 2021

That's right Anki Desktop does save images with their original names,

Anki Desktop work like this ->
image file name is not already present then it will simply include that image file with its original name example sample.png
and if filename is already present in collection then anki add unique code to original name like this sample-1ac0415bf8b772feb31e7ce8148cdd3210056d5e.png

@github-actions
Copy link
Contributor

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

@BrayanDSO
Copy link
Member Author

BrayanDSO commented Feb 10, 2022

Based on 2.16alpha42, this is what I got:

The method which handles selected files is handleSelectImageIntent. It gets the filename correctly, but changes it when running internalizeUri.

internalizeUri changes the filename when making a cache image copy with createNewCacheImageFile

To keep the filename, it would be necessary to change createNewCacheImageFile to something like:

private File createNewCacheImageFile(@NonNull String filename) throws IOException {
        File newImage = new File(mAnkiCacheDirectory, filename); 
        return newImage;
    }
// I'm new to Java, suggestions are welcome

or create a new method to be used at internalizeUri (which probably is better to avoid breaking other places which uses createNewCacheImageFile).

The only caveat with this method change would be drawings file names, which would change from the current imgRandomNumberSequence.extension pattern to unixtimestamp.extension.

My 1st doubt is which approach should be followed to naming drawings, since they aren't on Anki Desktop.

  • Keep using imgRandomNumberSequence.extension?
  • unixtimestamp.extension?
  • Some new pattern like draw-RandomNumberSequence.extension?
  • Whatever?
  • Something else?

Then, the second step would be checking if a media with that name already exists.

Anki desktop does with this function.
If new file, just add; if has the same filename and hash, do nothing; and if filename is equal but files are different, add its hash to the filename, like file-checksum.extension

Ankidroid does it with writeData. The main difference about its behavior it the filename pattern being file (index).extension when there are 2 or more file with the same name.

My 2nd doubt is why the pattern difference? I guess it's just lack of updating, since that part were added 8 years ago, but let me know if is there any specific reason. It should be updated to keep up with Anki Desktop?


Finally, ankidroid only handles adding media with same names. If two names are equal, the new one will have a index correctly added to its name. But the field <img> tag isn't updated, keeping the old name, since <img> tag and media adding are 2 separated steps. I'm still working out how to fix this. If anyone has any tips, let me know

@BrayanDSO BrayanDSO mentioned this issue Feb 11, 2022
7 tasks
@mikehardy mikehardy added this to the 2.16 release milestone Mar 1, 2022
@mikehardy
Copy link
Member

Hey @BrayanDSO solving your own issue from nearly a year ago, nice one!! :-)

@BrayanDSO
Copy link
Member Author

Fixing this is what made I start contributing to AnkiDroid, since attaching images is part of my workflow.

Back then when I created the issue, I hadn't thought it would be this easy to join a community and start pushing some bits of code. I'm happy I tried to look further into it and started contributing

@mikehardy
Copy link
Member

image

💥 🏆

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 a pull request may close this issue.

4 participants