Get all file's path from device

Get all file's path

package com.mihir.practice.extra;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.mihir.practice.R;
import java.io.File;
import java.util.ArrayList;
public class Main extends Activity implements OnClickListener {
private Button btn_get = null;
private ArrayList<String> mArrMainDir = null, mArrAllFileFolderPath = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_get = (Button) findViewById(R.id.btn_get);
// Comment below 1 line when- to get all directory including system directories
mArrMainDir = new ArrayList<String>();
mArrAllFileFolderPath = new ArrayList<String>();
btn_get.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get:
getStorageDirectory();
Log.d("List", "List = " + mArrMainDir.toString());
for (int i = 0; i < mArrMainDir.size(); i++) {
listFilesAndFilesSubDirectories(mArrMainDir.get(i));
}
break;
}
}
private void getStorageDirectory() {
// Primary physical SD-CARD (not emulated)
final String externalStorage = System.getenv("EXTERNAL_STORAGE");//internal
// All Secondary SD-CARDs (all exclude primary) separated by ":"
String secondaryStorages = System.getenv("SECONDARY_STORAGE");//physical SD card
if (secondaryStorages != null) {
if (secondaryStorages.contains(":")) {
secondaryStorages = secondaryStorages.substring(0, secondaryStorages.indexOf(":")).toString();
mArrMainDir.add(secondaryStorages);
// to get all directory including system directories
/*
String[] values = secondaryStorages.split(":");
mArrMainDir = new ArrayList<String>(values.length);
for (String s : values)
{
mArrMainDir.add(s);
}
*/
}
}
mArrMainDir.add(externalStorage);
}
/**
* List all files from a directory and its sub directories
*
* @param directoryName to be listed
*/
public void listFilesAndFilesSubDirectories(String directoryName) {
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
Log.d("FilesAndFilesSubDirectories", file.getAbsolutePath().toString());
mArrAllFileFolderPath.add(file.getAbsolutePath().toString());
} else if (file.isDirectory()) {
listFilesAndFilesSubDirectories(file.getAbsolutePath());
}
}
}
}
view raw Main.java hosted with ❤ by GitHub

For more refere the link

Comments

Popular Posts