droid Learner
droid Learner

Reputation: 48

Android getExternalStorageDirectory doesn't return any file

I'm new to android programming.
I'm trying to get the path to files on my nexus device by using getExternalStorageDirectory()
But it returns no file at all

Code:

File path = new File(Environment.getExternalStorageDirectory() +"");
path.mkdirs();
if (path.exists()) {
FilenameFilter filter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                File sel = new File(dir, filename);
                // Filters based on whether the file is hidden or not
                return (sel.isFile() || sel.isDirectory())
                    && !sel.isHidden();

            }
        };
String[] fList = path.list(filter);
// flist return null
}

Upvotes: 2

Views: 3240

Answers (2)

TheLittleNaruto
TheLittleNaruto

Reputation: 8473

As in your case you can fetch all data by doing following code:

ArrayList<String> filesList = new ArrayList<String>();   
String sd_card = Environment.getExternalStorageDirectory().toString();
file = new File( sd_card ) ;       
File list[] = file.listFiles();
for( int i=0; i< list.length; i++) {
    filesList.add( list[i].getName() );
}

Now filesList will have list of all files , you can use it as per your need.

Please don't forget to add permission in manifest:

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

Upvotes: 5

Akshat Agarwal
Akshat Agarwal

Reputation: 2847

Add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>" to your AndroidManifest.xml

Upvotes: 2

Related Questions