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());
}
}
}
Related
I am working on app that is required to read the recover to deleted data. I search a lot but could not find a single solution.
Then I got to know that there is a director "com.sec.android.gallery3d" where all images are saved in small thumbnails.
My question can I read images?
I checked this sample code. But its only reading those directories which is public like DCIM, notification,Down etc
File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
public ArrayList<File> listf(File xfile, ArrayList<File> files) {
// get all the files from a directory
File[] fList = xfile.listFiles();
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file, files);
}
}
return files;
}```
If we use the Android APIs to get the external storage directory, we get something like, /storage/emulated/0. If I run a recursive traversal on the same, I get a list of all the files and folders inside listed.
So, I thought of running the traversal on /storage thinking it would return all the public directories present. But, on running the same, I get a null list of Files inside it. For listing the files/folders, i have used the API: File.listFiles()
Why is this happening and how can I get a list of all publicly accessible files/folders on all the storages on Android?
I am using the below function for getting all the files of sdcard and its folder, subfolders. this works fine for me.
//A recursive function to list files of all folders and subfolders of SD Card
private void getAllFilesOfDir(File directory) {
Log.d("Spinner", "Directory: " + directory.getAbsolutePath() + "\n");
final File[] files = directory.listFiles();
if ( files != null ) {
for ( File file : files ) {
if ( file != null ) {
if ( file.isDirectory() ) { // it is a folder...
getAllFilesOfDir(file);
}
else { // it is a file...
Log.d("Spinner", "File: " + file.getAbsolutePath() + "\n");
allFileNameString.add(file.getName());
allFilePathString.add(file.getAbsolutePath());
}
}
}
}
}
Are you sure you gave correct path of sdcard storage directory, as the function call over here?
getAllFilesOfDir(Environment.getExternalStorageDirectory());
And try debugging inside the function, what goes wrong?
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.
I want to read all the music files from the sd card but I tried to put the file in several places on the SD card and the program still doesn't find the music on the card. Anyone any suggestions? Where exactly to put the files?
I was just doing that last night :).
In android, the sdcard is mounted on '/sdcard'. I don't have the code here, but it was something like this.
public List<String> getMp3Files() {
File sdcard = new File("/sdcard");
return getMp3Files(sdcard);
}
private List<String> getMp3Files(File directory) {
List<String> mp3files = new ArrayList<String>();
File[] files = directory.listFiles();
if(files == null) {
return Collections.EMPTY_LIST;
}
for( File file : files) {
if( file.isFile() && file.getName().toLowerCase().endsWith(".mp3")) {
mp3files.add(file.getAbsolutePath());
} else if ( file.isDirectory()) {
mp3files.addAll(getMp3Files(file));
}
}
return mp3files;
}
it's not optimized AT ALL for mobiles, but it works. And be careful that if you run this code starting on the root folder, it will end up in a infinite loop of folders! (you can always try :D).
Maybe this could help you
http://blog.jayway.com/2009/04/22/working-with-sd-cards-in-the-android-emulator/
I have an application that needs to read images from a folder created by the application on the sdcard(sdcard/"foldername"/"filename.jpg". I have no idea what the names of the files are because the user specifies the names of the files. I need to read the images from the folder and make something like the default image viewer. Im thinking read them into a grid view first but 1) cant figure out how to dynamically read them from a folder 2) how would I implement the image options like the default viewer? If there was a way to open the default viewer on a certain folder that would help.
any input would be amazing been working on it for a while.
Thanks
Here's how you can get a list of folders off of the memory card:
String state = Environment.getExternalStorageState();
if(state.contentEquals(Environment.MEDIA_MOUNTED) || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
String homeDir = Environment.getExternalStorageDirectory();
File file = new File(homeDir);
File[] directories = file.listFiles();
}
else
{
Log.v("Error", "External Storage Unaccessible: " + state);
}
This code is from the top of my head, so some syntax may be off a bit, but the general idea should work. You can use something like this to filter down the folders to only folders that contain images:
FileFilter filterForImageFolders = new FileFilter()
{
public boolean accept(File folder)
{
try
{
//Checking only directories, since we are checking for files within
//a directory
if(folder.isDirectory())
{
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) return false;
//For each file in the directory...
for (File file : listOfFiles)
{
//Check if the extension is one of the supported filetypes
//imageExtensions is a String[] containing image filetypes (e.g. "png")
for (String ext : imageExtensions)
{
if (file.getName().endsWith("." + ext)) return true;
}
}
}
return false;
}
catch (SecurityException e)
{
Log.v("debug", "Access Denied");
return false;
}
}
};
Then, change the first example to:
File[] directories = file.listFiles(filterForImageFolders);
That should return only directories that contain images. Hopefully this helps some!