0

My app saves voice recording on external storage. The user inputs a filename after the recording, and I add that file name to arraylist and save that arraylist using the SharedPreferences.Now, if a user goes to the directory and renames the file, the name of the file will not change in the SharedPreferences. This is a problem because when the list of files is shown in my app, I open it by using the name saved in SharedPreferences. So if a file is renamed, that name will not be in the SharedPreferences, and will crash my app. Is there anyway I can avoid this, for example - can I save a file and retrieve it and open it irrespective of what it's name is? How would I do that?

I'm surprised by the lack of similar questions, which makes me feel I'm missing an obvious easier way of saving and retrieving files.

  • Do you need to save the files on the external storage? If you can save them on the internal storage of your app then you wont have to worry about them to be renamed. – Jorge Morales Apr 23 '15 at 18:03
  • I want to store them on external storage, yes. –  Apr 23 '15 at 18:05
  • 1
    Fundamentally, the contract you make in using the External Storage is that other actors can modify whatever you put there. You can try to re-locate your files by size or fingerprint, or (to a limited degree) notice changes with a FileObserver, but ultimately if you want to protect the files, don't put them on the External Storage. On most modern devices the external and internal storage share the same pool of flash blocks, so there's not really any advantage to putting something there unless you specifically *want* to expose it to other actors. – Chris Stratton Apr 23 '15 at 19:49
  • Someone suggested meta tag. Is that a solution? @ChrisStratton –  Apr 23 '15 at 19:51
  • 1
    Scanning for unique data inside the files could be an option, if they have merely been moved or renamed, but it won't cover them being modified or deleted. And of course whatever you do, you need to make sure that your app does not crash simply based on a missing file, but rather fails gracefully by not performing that functionality and/or showing the user an informative dialog. – Chris Stratton Apr 23 '15 at 19:52
  • I've seen various apps save files on external storage. Would renaming their files break their app? If no, how do they manage to still keep a track? –  Apr 23 '15 at 19:53
  • Generally they do not try to keep track of that. But they also are written well enough that having a file missing causes a loss of functionality, rather than an actual crash. In most cases, files on external storage are either user's data which they may move or delete as desired, or data which the app has cached from a download and can be re-downloaded. – Chris Stratton Apr 23 '15 at 19:54

1 Answers1

0

This gives you a list of all files within a specified directory:

File directory = new File("/path/to/directory/");
File[] files = directory.listFiles();
for( File file : files ) {
    doStuffWithFile( file );
}

No need to know the file names, just the directory.

jadhachem
  • 1,123
  • 2
  • 11
  • 19
  • But to open them up, I'd need their name, right? What if the file was renamed? I wouldn't be able to query it using the named I saved it by. Or is there a better way of handling all of this? –  Apr 23 '15 at 18:08
  • All the files in the directory are now stored as `File` objects in the `files` array. Now you can just loop over them. I'll add some more code for clarity. – jadhachem Apr 23 '15 at 18:10
  • If the file is renamed then you cannot query files by name indeed. But do you have to? Why? You get here all files in the directory and can use every file as you want. – greenapps Apr 23 '15 at 18:12
  • Can I sort them by date created? –  Apr 23 '15 at 18:13
  • Yes. Look at file.getCreationDate() or somerhing like that. – greenapps Apr 23 '15 at 18:14
  • You could also try using the getHashCode, but not sure if it uses the file name to generate the hash – Jorge Morales Apr 23 '15 at 18:15
  • @greenapps There only exists an API for last modified time. Not for creation, unfortuntately. –  Apr 23 '15 at 18:16
  • Put the file name and file creation date IN the file. – greenapps Apr 23 '15 at 18:17
  • Check out [this question](http://stackoverflow.com/questions/2723838/determine-file-creation-date-in-java) for file creation date – jadhachem Apr 23 '15 at 18:17
  • @greenapps, it's an audio file. –  Apr 23 '15 at 18:20
  • You can set the file as reed only, does that works for you? https://developer.android.com/reference/java/io/File.html#setReadOnly() – Jorge Morales Apr 23 '15 at 18:21
  • I don't think that avoids renaming of file. @greenapps, thanks, I'll look into metatags. –  Apr 23 '15 at 18:24