Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android list files in private app storage

Tags:

android

save

load

I'm developing Android app, and I need save and load functions.

I want to save important data in private app storage with this function:

public static void save(Object file, String fileName, Context context)
        throws IOException, FileNotFoundException {
    FileOutputStream fos = context.openFileOutput(fileName,
            context.MODE_PRIVATE);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(file);
    oos.flush();
    oos.close();
    fos.close();
    System.out.println(TAG + ": File saved successful!");
}

I've also written method for loading data. This works because I know the exact name of file.

I need now to do dynamic saving of files, with dynamic generation of names. How can I list files saved in pricate app storage? So I can load exact one?

like image 787
Veljko Avatar asked Oct 21 '12 08:10

Veljko


1 Answers

Here you go!

File dir = getFilesDir(); 
File[] subFiles = dir.listFiles();

if (subFiles != null)
{
    for (File file : subFiles)
    {
        // Here is each file
    }
}
like image 140
Givi Avatar answered Sep 27 '22 21:09

Givi