Tutorials Android Listing all SDCard Filenames with extensions in android

Listing all SDCard Filenames with extensions in android

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

Total
0
Shares
guest
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Asad
Asad
8 years ago

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

javatechig
javatechig
8 years ago
Reply to  Asad

Thanks Asad.

Bac Nguyen
Bac Nguyen
8 years ago

Thanks for sharing!!

JavaTechig
JavaTechig
7 years ago

Thanks Walox for point it out

Shyam Gharat
Shyam Gharat
7 years ago

how to get file with extesion .thumbdata3– ?

chaa
chaa
7 years ago

thanx for the article

Divyanshu
Divyanshu
6 years ago

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

Ido G Zisman
Ido G Zisman
6 years ago

its not working for me 🙁

Rahul Srivastava
Rahul Srivastava
6 years ago
Reply to  Ido G Zisman

Use read external permission in manifest file.

Sanjeev
Sanjeev
6 years ago

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

Vijay kumar
Vijay kumar
5 years ago

Any link to list all internal thinks

raJ
raJ
5 years ago

How to open the listed files on click

Previous Post

Navigation Drawer Android Example

Next Post
Android Tutorials

Get Device ID Example in Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
12
0
Would love your thoughts, please comment.x
()
x