2

I am developing an application where I basically download a video file and show it in the HistoryActivity. My video model has name, authorName, authorImageUrl, videoDescription and every time I download a video I store the video info in the SharedPreference so that I can list all the videos with their description in the HistoryActivity. I am using video's name property as an ID to connect the video with its description I stored in the SharedPreference. However, when user changes the name of the video using other apps, I am losing the ID.

So the question: Is there any way that I can use as an ID for video file or any other file type?

amira
  • 416
  • 1
  • 7
  • 24

2 Answers2

2

You can generate hash for the file after which You can just lookup any file with matching hash.

However this should not be an issue, user renaming the file should be a rare occurrence and in that case why woudn't You just redownload the missing video.

Pasi Matalamäki
  • 1,843
  • 17
  • 14
  • This is likely the best solution, though a hash will change if the video file is edited. There may be other tricks to do with the metadata of the file, such as adding the description to the file's header, but I'm not familiar with how something like that would work. – Christopher Schneider Dec 29 '19 at 21:04
0

One solution is to generate a random ID using java.util.SecureRandom or another cryptographic RNG. For your purposes, a 32-bit (4-byte) ID should work well. Since it seems you will be storing video info locally on-device, it should be trivial for you to check for duplicate IDs.

See also "Unique Random Identifiers".

Peter O.
  • 32,158
  • 14
  • 82
  • 96