3

I wanted to see all the files I have in my external storage, I have this library that display the text to the user, but when I'm using it to show the sub files, it says something like :
(Ljava.File;@
How do I get it to show the name of the actual files to the user? Also, how can I show the name for a specific folder to the user? say file #3?

File[] files = myDir.listFiles();
UIHelper.displayText(this, R.id.textView1, files.toString());
2
  • Of course it is showing "Ljava.File;@", you are calling toString on a File Array. Is UIHelper a class of your own?
    – Blackbelt
    Apr 10, 2013 at 7:34
  • How do you display the array I guess? Apr 10, 2013 at 16:11

3 Answers 3

12

Checl if sdcard is mounted or not.

 if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
       ///mounted
}

Get the path of sd card

 File dir= new File(android.os.Environment.getExternalStorageDirectory());

Then call

  walkdir(dir);

 ArrayList<String> filepath= new ArrayList<String>();//contains list of all files ending with 

public void walkdir(File dir) {
 File listFile[] = dir.listFiles();

if (listFile != null) {
 for (int i = 0; i < listFile.length; i++) {

 if (listFile[i].isDirectory()) {// if its a directory need to get the files under that directory
  walkdir(listFile[i]);
} else {// add path of  files to your arraylist for later use

  //Do what ever u want
  filepath.add( listFile[i].getAbsolutePath());

 }
 }  
}    
}
4

This is the code for printing list of files and folders from ExternalStorage

String path = Environment.getExternalStorageDirectory().getAbsolutePath();

        Log.d("Files", "Path: " + path);
        File f = new File(path);        
        File file[] = f.listFiles();
        Log.d("Files", "Size: "+ file.length);
        for (int i=0; i < file.length; i++)
        {
            Log.d("Files", "FileName:" + file[i].getName());
        }

Don't forget to put below permission in you android Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1

You can use a recursive method to scan all your SD card:

String sdCardState = Environment.getExternalStorageState(); 
if( !sdCardState.equals(Environment.MEDIA_MOUNTED ) ) {
    //displayMessage("No SD Card.");
    return;
} else {
    File root = Environment.getExternalStorageDirectory();
    lookForFilesAndDirectories(root);
}

// lookForFilesAndDirectories() method:

public void lookForFilesAndDirectories(File file) {
    if( file.isDirectory() ) {
        String[] filesAndDirectories = dir.list();
        for( String fileOrDirectory : filesAndDirectories) {
            File f = new File(dir.getAbsolutePath() + "/" + fileOrDirectory);
            lookForFilesAndDirectories(f);
        } 
    } else {
        doSomethingWithFile(f);
    }
}
1
  • 1
    I like this recursive method. Two minor errors to fix in the code - dir should be file
    – Mick
    May 30, 2015 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.