Android Read File Names SDCard [duplicate] - android

This question already has an answer here:
Find all files on sd card
(1 answer)
Closed 8 years ago.
I want to get file names stored in sd card android, but i could not find a proper solution.
Can somebody please help me??
public GetFileNames(Context context) {
// TODO Auto-generated constructor stub
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard, "path");
String name;
GenerateAlertBox alert = new GenerateAlertBox();
for (File f : dir.listFiles()) {
if (f.isFile()){
name = f.getName();
// do whatever you want with filename
alert.GenerateAlertBoxes(name, context);
}
}
}
I found this,but I don't the the path.

If you want to find all the files in the top folder of the SDCard you can do something like this:
public GetFileNames(Context context) {
File dir = Environment.getExternalStorageDirectory();
ArrayList<String> fileNames = new ArrayList<String>();
for (File f : dir.listFiles()) {
if (f.isFile()){
name = f.getName();
fileNames.add(name);
}
}
//Do whatever you want with fileNames array.
}
EDIT: If you want to get your apps private filenames you should do this:
public GetFileNames(Context context) {
File dir = context.getFilesDir();
ArrayList<String> fileNames = new ArrayList<String>();
for (File f : dir.listFiles()) {
if (f.isFile()){
name = f.getName();
fileNames.add(name);
}
}
//Do whatever you want with fileNames array.
}

Related

Activity to search all music files (or a particular extension) in Android storage

I am building an Android app that writes files in the format of my custom .fire extension.
I need to access them all at once and put them in a ListView. Could someone please help me to fetch them via an activity or Async Task Manager?
You can write an Async Task to fetch the .fire files from the folder into which you are writing them initially and dynamically add the filenames into the listview.
You can do something like this in your async task:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/"+FolderWhichContainsyourFiles);
File[] listOfFiles = dir.listFiles();
for (int count = 0; count < listOfFiles.length; count++) {
File file = listOfFiles[count];
if (file.isFile() && file.getName().endsWith(".fire")) {
String fileName = file.getName();
//Here you can dynamically add this name into the ListView
}
}
}
You need to use an ArrayAdapter and add elements to the ArrayAdapter dynamically. It is brilliantly explained here: Dynamically add elements to a listView Android
if you want to find files recursively:
private static List<File> filesList = new ArrayList<File>();
public static List<File> recursiveList(String filePath) {
try {
File[] dir = new File(filePath).listFiles();
for (File item : dir) {
if (item.isDirectory()) recursiveList(item.getAbsolutePath());
else if (item.isFile() && item.getName().endsWith(".fire"))
filesList.add(item);
}
return filesList;
} catch (Exception e) {
return null;
}
}
and to retrieve files in activity:
List<File> files = recursiveList("/sdcard/Music/");
for (File file : files)
adapterFilesList.add("" + file.getName());

Android: How to list all the files in a directory in to an array?

I have a variable "directorypath". It has the path to a directory in sd card.
Ex: "sdcard/images/scenes"
Now, I want to list all the files in the "directorypath" and want an Array to store all the file names in it.
Ex: Array[0]= Filename1, Array[1]= Filename2 Etc...
I tried something like Array[] myArray = directorypath.listfile(); but didn't work..!
Can anyone give me the code or at least help me? Much appreciated.
I think the problem you are facing is related to the external storage directory file path. Don't use whole path as variable if you can access with environment.
String path = Environment.getExternalStorageDirectory().toString()+"/images/scenes";
Also, you can use the API listfiles() with file object and it will work. For eg ::
File f = new File(path);
File file[] = f.listFiles();
Try the following code which may help you
List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
inFiles.add(file);
}
}
return inFiles;
}
This will return you list of files under the directory you specified.
From the File object in list.. you will get all information about files you required.
Try the following code:
File externalStorageDirectory = Environment.getExternalStorageDirectory();
File folder = new File(externalStorageDirectory.getAbsolutePath() + "/FilePath");
File file[] = folder.listFiles();
if (file.length != 0) {
for (int i = 0; i < file.length; i++) {
//here populate your list
}
} else {
//no file available
}
According to your error message "Cannot resolve listFiles()", you are trying to call a method on a String object, when you actually want to be calling that on a File object.
Create a new File object,
String parentDirectory = "path/to/files";
File dirFileObj = new File(parentDirectory);
File[] files = dirFileObj.listFiles();
I was indeed calling a function for improper data type. i.e., listfiles() won't work on String type variable..!!
SO, I created a File type variable and a File[] array to pass the list of files..!
This solved the issue.
its very simple Just two lines of code
File mListofFiles = new File(Environment.getExternalStorageDirectory.getAbsolutePath()+"/",childpath:Foldername);
File f = new File(mListofFiles.toString());
File file[] = f.listFiles();

How to read file names of the files that are present in the android's Internal/External storage

I am trying to program my own file manager for android. What I want to know is that how can I read the file names of all the files present in external/internal storage?
I want to read the file names and show them in a listview so the user could see what files are present in what folder. I know this thing is going to work in recursive manner as I have to read the content of sub directories as well.
This is what you have to do. Before going to write please refer the File class in java. This will help you to clear lot of things.
Below is the snippet that provides the list of files.
File directory = new File(path);
File[] listFiles = directory.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (file.isDirectory())
// do the stuff what you need
else if (file.isFile()) {
// do the stuff what you need
}
}
}
}
Following code will give you list of files in android sdcard:
/**
* Return list of files from path. <FileName, FilePath>
*
* #param path - The path to directory with images
* #return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"
*/
private List<String> getListOfFiles(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("jpeg", "jpg",
"png", "bmp", "gif");
#Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
You can call same method to get sub directory file also.

How to access all 3GP audio files from SDcard

This is how i tried to access files from SDcard
// Use the current directory as title
path = "/sdcard/";
if (getIntent().hasExtra("path")) {
path = getIntent().getStringExtra("path");
}
setTitle(path);
// Read all files sorted into the values-array
final List values = new ArrayList();
File dir = new File(path);
if (!dir.canRead()) {
setTitle(getTitle() + " (inaccessible)");
}
final String[] list = dir.list();
if (list != null) {
for (String file : list) {
if (!file.startsWith(".")) {
values.add(file);
}
}
}
Collections.sort(values);
but not getting how to display only 3Gp vales in list ,anyone suggest me to how to do it.
Thank you.
Have you tried just checking to see if the file has a .3gp extention?
Like:
if(file.contains(".3gp"))?

Get filenames from a directory in Android

I want to populate a spinner with the filenames of files found on the SD card with specific extensions. I can get it thus far to populate the spinner with the correct files, but the path is shown as well.
Can anyone tell me how to only get the filename of a specific file in a directory on the SD card?
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "path");
for (File f : yourDir.listFiles()) {
if (f.isFile())
String name = f.getName();
// Do your stuff
}
Have a look at Environment page for more info.
Try below code
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard, "yourpath");
for (File f : dir.listFiles()) {
if (f.isFile())
String name = f.getName();
// do whatever you want with filename
}
File filePath= new File(File Address);
File[] fileList = filePath.listFiles();
String name = fileList [0].getName().toString();
could you process the string in reverse order(right to left), finding the first slash, then cutting the string at that point and taking the rightmost part of the string as the filename ?
use method getName() of file object:
file.getName();
Above answers are giving null pointer exception in my case. Following code worked for me:
File yourDir = new File(Environment.getExternalStorageDirectory().getPath() + "/WhatsApp/Databases");
for (File f : yourDir.listFiles()) {
if (f.isFile())
name = f.getName();
// Do your stuff
}

Categories

Resources