• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Writing filename in sequence?

ar792

Newbie
Oct 10, 2015
14
1
In my app a video is generated who's name is

private file = new File(Environment.getExternalStorageDirectory(), "/videos/" + "file " + ".mp4");

If i start recording again , it overwrites the above file,
So, i need to give it a sequence; like (file + 1); so i get like file1, file2, file3, .... so on
how do i do it?

Secondly, i need to merge all this files in a mp4parser code,

public class Append {
public static void main(String[] args) throws IOException {
String[] videoUris = new String[]{
//.........code for files to be merged........//
}

so how do i feed the above files to it, in the sequence which was created prior?

Thanks........!
 
Last edited:
you could use random or you could count the file if(filename exist){ newfile+1
}

Code:
   File checkfile = new File(file);
int nextfile = 0;
if (!checkfile.exists())
{
 

             //the file dose not existe create it      

}
else
{
nextfile++;
//the filename is already stored so lets add more to the new one

//filename+nextfile+".mp4"
}

you might want to strip the file apart to build it the way you want, because if the file name is return back to you as example file.mp4 you do not want to do this file.mp41 so I would strip the file to get just the name not the extinction so I know the file name is file then I would + my next file so it returns file1 then I would add my end extinction so my file ends up being file1.mp4


Code:
String newString13 = file.replace(".mp4", "");
Log.i("log", "whats the name of the file? " + newString13);

now doing it this way I see your going to end up getting a lot of files like this

file1.mp4
file11.mp4
file111.mp4
because the count is always going to be 1 if you only use it once and then turn off the app and do it again but it would count if you did it say three times in a row

so I would then end up making a new patch to get the file number but if your dealing with large numbers this could be a pain so I would set a identifier to get the number say filename is Macks pit bull bite.mp4 I would set it as Macks pit bull bite-1.mp4
this way I could pinpoint the - in the filename and read from there so say my file name was

Macks pit bull bite-1203940.mp4

I could only get just the number and the ending, and then strip the ending of the .mp4
and return just the number as 1203940 so I could then set the nextfile INT so it would not == 0 but it would == 1203940 and when I add the nextfile++ it would then === 1203941
 
Last edited:
  • Like
Reactions: ar792
Upvote 0
I tried with this:

int nextfile = 0;
File file = new File(Environment.getExternalStorageDirectory(), "/videos/" + "file" + Integer.toString(nextfile) + ".mp4");
File checkfile = new File(String.valueOf(file));

if (!checkfile.exists())
{
try{
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

}
else
{
nextfile++;
}

i only get file0.mp4, which is overwritten..

i am going wrong somewhere above...

Kindly help...!
 
Upvote 0
Awesome!.. It worked perfectly!!

Can you help me in the second part?

https://github.com/sannies/mp4parse...a/com/googlecode/mp4parser/AppendExample.java

i have to put these 'n' no. of files in the parsing code, so how should it be ?
After searching on web, what i have found is to generalize the reference for above created files; i need like "/video/file%d"

So I have tried the following:
public class AppendExample {
public static void main(String[] args) throws IOException {
String[] videoUris = new String[]{
//...here i need to place the files orderly starting from file1,2..
"/video/file%d"
}

But this is not working when i run the app, nothing happens, nor anything is shown on console..

Thanks..!
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones