Question

I want to get file names stored in sd card android, but i could not find a proper solution. Can somebody please help me??

public GetFileNames(Context context) {
        // TODO Auto-generated constructor stub
        File sdCard = Environment.getExternalStorageDirectory();

        File dir = new File(sdCard, "path");
        String name;
        GenerateAlertBox alert = new GenerateAlertBox();
        for (File f : dir.listFiles()) {
            if (f.isFile()){
                name = f.getName();
                // do whatever you want with filename
            alert.GenerateAlertBoxes(name, context);
            }
        }
    }

I found this,but I don't the the path.

Was it helpful?

Solution

If you want to find all the files in the top folder of the SDCard you can do something like this:

public GetFileNames(Context context) {
    File dir = Environment.getExternalStorageDirectory();

    ArrayList<String> fileNames = new ArrayList<String>();
    for (File f : dir.listFiles()) {
        if (f.isFile()){
            name = f.getName();
            fileNames.add(name);
        }
    }
    //Do whatever you want with fileNames array. 
}

EDIT: If you want to get your apps private filenames you should do this:

public GetFileNames(Context context) {
    File dir = context.getFilesDir();

    ArrayList<String> fileNames = new ArrayList<String>();
    for (File f : dir.listFiles()) {
        if (f.isFile()){
            name = f.getName();
            fileNames.add(name);
        }
    }
    //Do whatever you want with fileNames array. 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top