i need to list only the files present on a sdcard.
With the following code:
File sdcard=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if(sdcard.isDirectory()){
String files[]= sdcard.list();
for(int i=0;i<files.length;i++){
File f=new File(files[i]);
if(!f.isDirectory())
Log.d("FILES",files[i]);
}
}
I see also in the log the subdirectories. What am i doing wrong?
Try this:
if(sdcard.isDirectory()){
File[] files = sdcard.listFiles();
for (File f : files){
if(!f.isDirectory())
Log.d("FILES",f.getName());
}
}
The key difference is sdcard.files() vs sdcard.listFiles().
I think the problem is that you need to do this recursively:
File sdcard=Environment.getExternalStorageDirectory();
private void logFiles(File sdcard) {
if(sdcard.isDirectory()){
File[] files= sdcard.listFiles();
for(int i=0;i<files.length;i++){
if(!f.isDirectory())
Log.d("FILES",files[i]);
else
logFiles(files[i]);
}
}
}
I haven't tested this out, but the last else is probably what you were missing and you may find listFiles to be a better choice, here, but, you will need to log the filename, not the File as I left here.
Related
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()
I have Sony Xperia M2Dual Android 4.4.4. I try get path to my sdcard.
All paths what I tried get
/mnt/*
/storage/*
And others
File[] list = (new File("/mnt/sdcard")).listFiles();
for(File f : list){
Log.i("bairro", f.getPath());
}
I get error NullPointException.
Rather than using /mnt/sdcard use
File[] list = (new File(Environment.getExternalStorageDirectory().toString())).listFiles();
and also need permission for External storage access in manifest
TRY THIS:
File[] list = (new File(Environment.getExternalStorageDirectory().getAbsolutePath())).listFiles();
Log.d("Length", String.valueOf(list.length));
for(File f : list){
Log.i("bairro", f.getPath());
}
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();
Plzzz help me with this. Already late searching for the solution:
I want to list each and every Folder and file which is in the Android Phone's Internal Memory and External Memory.
The below code only gets list of files in one single directory.. But I'm unable to understand how would I list all the folders and files from internal and external memory.
..... List files = getListFiles(new File("YOUR ROOT")); ....
private List getListFiles(File parentDir) {
ArrayList inFiles = new ArrayList();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".csv")){
inFiles.add(file);
}
}
}
return inFiles; }
Any help?
Look at the File object. It gives you methods to get all the files in a directory, tells you whether a file is a directory, and many other things.
You could easily do a recursive tree walk of the directory system. You probably will run into permissions issues, though, unless you have "rooted" your device.
I finally found this somewhere else.. This is how it's done (fully functional code)
public void walk(File root) {
File[] list = root.listFiles();
for (File f : list) {
if (f.isDirectory()) {
Log.d("realcrazy", "Dir: " + f.getAbsoluteFile());
walk(f);
}
else {
Log.d("realcrazy2", "File: " + f.getAbsoluteFile());
}
}
}
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.