how to access directory list in android - android

i want to access the directory structure in my application

try
File f = new File(Environment.getExternalStorageDirectory());
File[] files = f.listFiles();

File mfile=new File("/sdcard/");
File[] list=mfile.listFiles();
System.out.println("list"+mfile.listFiles().length);
for(int i=0;i<mfile.listFiles().length;i++)
{
System.out.println("hidden path files.."+list[i].getAbsolutePath());
}
try this

Related

How to find directories with particular name

Any app can create folder in external storage.
This folder can be placed at SD card or phone inner memory.
If i use this code, i can to get list of all my external storages.
File file2[] = getExternalFilesDirs(null);
it return:
/storage/emulated/0/Android/data/com.snailp4el.android.tatoeba/files
/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files
but if i try to get list of it path:
File f = new File("/storage/8639-0FFD/Android/data/com.snailp4el.android.tatoeba/files");
File[] files = f.listFiles();
I get nothing.
in this case:
File f = new File("/storage/");
File[] files = f.listFiles();
i get:
/storage/8639-0FFD
/storage/emulated
/storage/self
That is OK. i finally get all storages in my phone.
Question is:
How to scan subfolders to find particular folders by name?
And is my approach right. I mean to scan "/storage"?
And if Yes will it work at all version?
and finally what is "/storage/self"?
I solved it this way.
May by it is not the best solution but it works fine for me.
public ArrayList getExternalFolderPath(String folderName){
ArrayList<String> arrayList = new ArrayList<>();
//get all storages in phone
File externalStorages[] = getExternalFilesDirs(null);
for (File es: externalStorages){
String exF = es.toString();
//cut out what don't need
exF = exF.split("/Android/data")[0];
File file = new File(exF);
File[] files = file.listFiles();
for (File f: files){
Log.i(TAG, "getAnkiExternalFolder()" + f.toString());
//add in array the path
if(f.toString().contains(folderName)){
arrayList.add(f.toString());
}
}
}
return arrayList;
}
You can get two directories(from both storage). The directory you need the last changed one.
file.lastModified()

android File array object is giving null

File file=Environment.getExternalStorageDirectory().getAbsoluteFile();
File[] ff=file.listFiles();
Here ff is giving me null instead of list of sdcard folders.
Use this mehod to get list of all files from external storage directory
File sdcard = Environment.getExternalStorageDirectory();
File dirs = new File(sdcard.getAbsolutePath());
if(dirs.exists()) {
File[] files = dirs.listFiles();
}

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 get the name of all image files in a folder?

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

Android SD card directory folders name list

How can I get names of all folders (not files) in specific directory on SD card? For example all subfolders names in /sdcard/first_level_folder/....
I need folder name, so I can pass complete path (string) to the method which will then compress (zip) it.
Thanks.
I think what you are looking for is
File dir = new File("directoryPath");
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] files = dir.listFiles(fileFilter);
Step #1: Use Environment.getExternalStorageDirectory() to get the root of external storage. /sdcard/ is incorrect on most devices.
Step #2: Use the appropriate File constructor to create the File object pointing to your desired directory inside external storage.
Step #3: Use Java I/O to find what is in that directory.
Well you can use something like:
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) { ... do stuff }
}
well this is a java related question.
Check this out.
To get access to the sdcard folder name :
File extStore = Environment.getExternalStorageDirectory();
String SD_PATH = extStore.getAbsolutePath()+"your sd folder here";
File file=new File("/mnt/sdcard/");
File[] list = file.listFiles();
int count = 0;
for (File f: list){
String name = f.getName();
if (name.endsWith(".jpg") || name.endsWith(".mp3") || name.endsWith(".some media extention"))
count++;
System.out.println("170 " + count);
}
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file) {
if (f.isDirectory()) {
// ... do stuff
}
}
Step #1: Use Environment.getExternalStorageDirectory() to get the root of external storage. /sdcard/ is incorrect on most devices.
Step #2: Use the appropriate File constructor to create the File object pointing to your desired directory inside external storage.
Step #3: Use Java I/O to find what is in that directory.

Categories

Resources