I would like to know if there is a possibility of showing some XML files which are in a specific folder, in a ListView. I don't want to show the info from those XML files, just the file names. For example if there are 3 files called "car.xml", "dog.xml" and "cat.xml" show in a ListView "Car, Dog , Cat.
Thank you for your time.
Try something like this :
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "path");
for (File f : yourDir.listFiles()){
if (f.isFile()){
if(f.getName().endsWith(".xml") || f.getName().endsWith(".XML")) {
String filename = f.getName();
String filenameArray[] = filename.split("\\.");
String name = filenameArray[filenameArray.length-1];
xmlFilesList.add(name);
}
}
}
Related
I saved some images in my storage's specific folder.I have fro example 1.png,2.png and etc. I know how i can get all files's name.This is source
private void getAllElemetsFromStorage() {
String root_sd = Environment.getExternalStorageDirectory().toString();
File file = new File(root_sd + "/myfolder");
for (File f : file.listFiles()) {
if (f.isFile()) {
String name = f.getName();
Log.e("file names", name + "");
}}
}
This code working correct but i have one problem.I have some json and in my json i have file's name.How i can get all files name witch is not in my json and is my directory and how i can delete all this files? My goal is to save only files,witch are in my jsons and i want to delete all unused files from my directory
How i can solve my problem?
thanks
Inside your if statement, after you get the file name, loop through the json and check whether its in there or not. If not, delete it.
Your for loop that exists already will do this for every file in the directory, removing it if the name doesnt exist in the json.
Below is an example. Depending on your Json, you need to build a list or loop through another way.
private void getAllElemetsFromStorage() {
String root_sd = Environment.getExternalStorageDirectory().toString();
File file = new File(root_sd + "/myfolder");
ArrayList<String> jsonFileNames = // Get list of file names from json
for (File f : file.listFiles()) {
if (f.isFile()) {
String name = f.getName();
// If not found in json. Delete file
if(!jsonFileNames.contains(name))
f.delete();
}
}
}
Note: this code is untested so may be typos. But it should give you the idea.
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());
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();
I want to get the names of all image file in a directory (lets say pictures) into an array of strings. I'm still new so I don't know how to approach this. I just need a way to retrieve the filenames with the .png extension from the pictures folder on the sd card so I can store it in an array.
this is how to list files under any path.
private void listAllFiles(String pathName){
File file = new File(pathName);
File[] files = file.listFiles();
if(files != null){
for(File f : files){ // loop and print all file
String fileName = f.getName(); // this is file name
}
}
}
You can do this using the java.io.File
If you just want the names you can use.
File dir = new File("<YourPath>");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(dir.list()));
If you want the whole file object use.
File dir = new File("<YourPath>");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles()));
More Information
java.io.File
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
}