0

I am working on this sound recorder activity. I added a restart button to an activity to start recording again, but the problem with the code is it overwrites the new record file over the last file. I want to edit this part of the code.

outputFile = Environment.getExternalStorageDirectory().
    getAbsolutePath() + "/Recording" + date + time + ".3gpp";

The problem is, I am a noob at coding and I don't know how to explain date and time and I don't know if it works or not.

dda
  • 6,030
  • 2
  • 25
  • 34
  • Change `date + time` to `(date.getTime()+Math.random())` to get a unique name - as per http://stackoverflow.com/questions/20120562/javascript-timestamp-number-is-not-unique – mplungjan Dec 08 '16 at 07:58

4 Answers4

1

How about something like

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String fName = String.format ("myFile_%s.txt", sdf.format(new Date ()));

And thanks to @FabianGünter for making this a complete solution with

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); 
outputFile = Environment.getExternalStorageDirectory(). 
             getAbsolutePath() + "/Recording" + sdf.format(new Date()) + ".3gpp";
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I'll hack this answer to provide a full code example according to the code you gave: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); outputFile = Environment.getExternalStorageDirectory(). getAbsolutePath() + "/Recording" + sdf.format(new Date()) + ".3gpp"; – N4zroth Dec 08 '16 at 08:56
  • @FabianGünter Thanks I have edited my answer with your input. – Scary Wombat Dec 09 '16 at 00:02
0

If you just need a unique name you could use .

You'd use it to replace date and time. getAbsolutePath() + "/Recording" + System.currentTimeMillis() + ".3gpp";

It gives the number of milliseconds since January 1st 1970. Which is generally going to be a uid. And will certainly create unique and sequential names.

Tatarize
  • 10,238
  • 4
  • 58
  • 64
0

Use This function it will return you a unique file path with date and time

public String getUniqueFilePath(String name,String formate) {
    photo = new File(Environment.getExternalStorageDirectory(),
            new SimpleDateFormat("yyyy_MM_dd_HHmmss",
                    Locale.getDefault()).format(new Date()) + name + formate);
    return photo.getPath();
}
Tabish Hussain
  • 852
  • 5
  • 13
0

thanks a lot my friends.but i found the answer myself. add:

import java.util.Calendar;

then add:

Calendar cal = Calendar.getInstance(); 
int seconds = cal.get(Calendar.SECOND);
int minutes = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR);
int date = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

and then:

outputFile = Environment.getExternalStorageDirectory().
       getAbsolutePath() + "/Recording" + (year) + "-" + (month) + "-" + (date) + "-" + (hour) + "-" + (minutes) + "-" + (seconds) + ".3gpp";

its worked for me.