0

I have an app. Whenever a user records a song in my app, i am creating a file in sdcard with a name which has time stamp in it. The time stamp is created using System.currentTimeMillis. I am reading those files in Other Activity. Suppose user recorded one file and then went to the sdcard and copied and pasted the first file to generate other files in the sdcard. Now all the files have same System.currentTimeMillis. Now when i go to my other activity, i am not able to see any of my files. How can i prevent such behaviour of copying pasting the same file with same time stamp. Any help would be greatly appreciated.

  • post your code please. – Rumit Patel Feb 10 '18 at 06:15
  • @Rumit Please have a look –  Feb 10 '18 at 06:17
  • you can't prevent such behaviour of copying pasting the same file with same time stamp. at least when file is on SD-card. you can think alternate way. internal-storage or other. – Rumit Patel Feb 10 '18 at 06:26
  • @Rumit Even in internal storage user can copy pate the file –  Feb 10 '18 at 06:27
  • `Now all the files have same System.currentTimeMillis.` ??? That time was in the file name. I cannot imagine that if you copy a bunch of files their file names would change. Unclear where you complain about. – greenapps Feb 10 '18 at 09:30
  • `How can i prevent such behaviour of copying pasting the same file with same time stamp.`. Your post does not make sense. You will not start two recordings at the same time. So there will never be files with the same timestamp in the file name. Are not you mixing up with file modified time? – greenapps Feb 10 '18 at 09:36
  • Further there can not be more files with the same file name in a directory. – greenapps Feb 10 '18 at 09:38

2 Answers2

0

You can't prevent such behaviour of copying pasting the same file with same time stamp. at least when file is on SD-card.

Other solution you can try is using HashMap, instead of ArrayList.

You can use

HashMap<String, MyRecording> mHashMap = new HashMap<String, MyRecording>();

instead of

ArrayList<MyRecording> arrayList = new ArrayList<MyRecording>();

In HashMap, you can set your fileName as key. so it will not repeated.

For iterating on hashmap, you can have look at How to efficiently iterate over each entry in a 'Map'?

Happy coding :-)

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
0

does android system even allow for two files to exist in the same place with same identical names? that's strange, i never tried that before.

anyway I will give you my code how i create a file with a unique filename and how i load this file names in a list to show them in a listview, hope that can help you to fix your problem.

First I make a unique file name, btw AUDIO_RECORDER_FOLDER is a string = "my records folder"

    private String getFilename() {
    filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);
    if (!file.exists()) {
        file.mkdirs();
    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss");
    String currentDateTimeString = simpleDateFormat.format(new Date());
    //String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

    return (file.getAbsolutePath() + "/" + currentDateTimeString + AUDIO_RECORDER_FILE_EXT);
}

then when i start recording i call it to create a filename:

String filename = getFilename();
...
fileOutputStream = new FileOutputStream(filename, true);
...(some codes)

then i load them from user sd card if he has one or from internal storage if he doesn't have sd card like this:

filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/my records folder";
File file = new File(filepath);
files = file.list();
if(files != null && files.length>0){
    records = new ArrayList<>();
    for(int i =0; i<files.length; i++){
       Records record = new Records();
       record.setTitle(files[i]);
       records.add(record);
    }
}

then i pass the records arraylist to my adapter and that's it.

Daiwik Dan
  • 195
  • 10