Here in this example we will see how to list all files present in SDCard of your android device. In the below example it searches for all of the SDCard folders and lists all of the images available. You may extend this example to list other file formats.

Steps

  1. First let us create a new Android project and name it as you like. This example uses minSdkVersion=8 and ¬†targetSdkVersion=”18″ and tested over HTC Onex (Android 4.2) device.
  2. Make sure, you have SDCard with some files to list.

Android Activity Layout (activity_main.xml)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="8dp" >
    </LinearLayout>

</ScrollView>

From the above code snippet, you can observe that I have taken a ScrollView and a LinearLayout as its child. This LinearLayout will hold all of the TextViews that shows filenames.

Android Activity (MainActivity.java)

package com.javatechig.listallfiles;

import java.io.File;
import java.util.ArrayList;
import com.example.listallfiles.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private File root;
	private ArrayList<File> fileList = new ArrayList<File>();
	private LinearLayout view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		view = (LinearLayout) findViewById(R.id.view);

		//getting SDcard root path
		root = new File(Environment.getExternalStorageDirectory()
				.getAbsolutePath());
		getfile(root);

		for (int i = 0; i < fileList.size(); i++) {
			TextView textView = new TextView(this);
			textView.setText(fileList.get(i).getName());
			textView.setPadding(5, 5, 5, 5);

			System.out.println(fileList.get(i).getName());

			if (fileList.get(i).isDirectory()) {
				textView.setTextColor(Color.parseColor("#FF0000"));
			}
			view.addView(textView);
		}

	}

	public ArrayList<File> getfile(File dir) {
		File listFile[] = dir.listFiles();
		if (listFile != null && listFile.length > 0) {
			for (int i = 0; i < listFile.length; i++) {

				if (listFile[i].isDirectory()) {
					fileList.add(listFile[i]);
					getfile(listFile[i]);

				} else {
					if (listFile[i].getName().endsWith(".png")
							|| listFile[i].getName().endsWith(".jpg")
							|| listFile[i].getName().endsWith(".jpeg")
							|| listFile[i].getName().endsWith(".gif"))

					{
						fileList.add(listFile[i]);
					}
				}

			}
		}
		return fileList;
	}

}

Output

Example Listing All Files In Android

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Related Articles

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • Vijay kumar

    Any link to list all internal thinks

  • Sanjeev

    I like this example because i need, can you extend this as how we can select all these file for sending other..

  • Ido G Zisman

    its not working for me :(

    • Rahul Srivastava

      Use read external permission in manifest file.

  • Divyanshu

    This does not give all the images in sd card , whatsapp profile images are not visible , Whatsapp send files are not visible , and many other files are also not visible with this code , please help regarding that

  • chaa

    thanx for the article

  • Shyam Gharat

    how to get file with extesion .thumbdata3– ?

  • Thanks Walox for point it out

  • Bac Nguyen

    Thanks for sharing!!

  • Asad

    That is awesome tutorial… It’s save my time… Thanks for sharing..