I am using below code to fetch folders from SD Card and also deleting empty folders
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// delete empty folder
if (inFile.listFiles().length == 0) {
inFile.delete();
}
// add names into an ArrayList and populate ListView.
listingSDCard.add(inFile.getName());
}
}
but i don't know how to refresh listview, because i noticed that empty folder has been deleted programmatically from SD Card, but still visible into list, i am using simple list, i know notifyDataSetChanged() help us to refresh list.
my complete code:
final ListView listView = (ListView) findViewById(R.id.listViewAnimals);
List<String> listingSDCard = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory().toString()+"/Church Application/";
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// delete empty folder
if (inFile.listFiles().length == 0) {
inFile.delete();
}
// add names into an ArrayList and populate ListView.
listingSDCard.add(inFile.getName());
}
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.activity_archive_list,
listingSDCard);
listView.setAdapter(arrayAdapter);
Edited code:-
final ListView listView = (ListView) findViewById(R.id.listViewAnimals);
List<String> listingSDCard = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory().toString()+"/Church Application";
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.activity_archive_list,
listingSDCard);
File f = new File(path);
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// delete empty folder
if (inFile.listFiles().length == 0) {
inFile.delete();
arrayAdapter.notifyDataSetChanged();
}
// add names into an ArrayList and populate ListView.
listingSDCard.add(inFile.getName());
}
}
listView.setAdapter(arrayAdapter);
Issue still not resolved - still getting empty folder name into ListView, whereas folder not present in SD Card !
Try this:
for (File inFile : files) {
if (inFile.isDirectory()) {
// delete empty folder
if (inFile.listFiles().length == 0) {
inFile.delete();
} else {
// add names into an ArrayList and populate ListView.
listingSDCard.add(inFile.getName());
}
}
}
Although you are deleting the folder in the system, you are adding it to your listingSDCard, you need to put it in an else.
Call notifyDataSetChanged() on your Adapter.
code:
arrayAdapter.notifyDataSetChanged();
Also you can use this:
arrayAdapter.invalidateViews();
arrayAdapter.notifyDataSetChanged();
add this line after delete folder.
Related
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 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.
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"))?
I want to list only .db files in a folder and join them to one db file..At this time I am just able to list all files in my folder..Can some one help me how to filter .db files?and then whats the best way to join this database files in one file?
Here is my code
public List<String> directoryPath(File path) {
List<String> item = null;
if( path.exists() && path.isDirectory()) {
File[] files = path.listFiles(new FilenameFilter(){
public boolean accept(File dir, String filename) {
return filename.endsWith(".db");
}
});
for(int i=0; i < files.length; i++)
{
File file = files[i];
item.add(file);
}
}
return item;
}
I edited my code and now I get null ponter access in item Can some one helps me abut this?why I get this error?
I call this code with this way
File path=new File(ClubCP.SDcardPath);
Log.i("database_list","call list files");
List<String> file_lists = main.directoryPath(path);
Log.i("database_list","list adapter");
ListView Database_list=(ListView)findViewById(R.id.database_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
file_lists);
Database_list.setAdapter(adapter);
You implement the FileFilter (or FilenameFilter) interface and pass that object to the File.listFiles(File[name]Filter) method.
public void directoryPath(File path) {
if( path.exists() && path.isDirectory()) {
File[] files = path.listFiles(new FilenameFilter(){
public boolean accept(File dir, String filename) {
return filename.endsWith(".db");
}
});
// files only contains files that end with .db
}
}
To merge database files you're not really on the right track. You can't just append one database file to the end of the other. You have to actually load one file using sql and then copy the tables ano in sql. How can I merge many sqlite databases?
check if files[i].getName().endsWith(".db")
I'm wondering how to show files from a directory in a ListView`. The files can be listed with:
File dir = new File(dirPath);
File[] filelist = dir.listFiles();
and added to the ListView via ArrayAdapter but I don't get the usage of the ArrayAdapter.
I guess you want to show the names of the files from that directory so you could try this:
File dir = new File(dirPath);
File[] filelist = dir.listFiles();
String[] theNamesOfFiles = new String[filelist.length];
for (int i = 0; i < theNamesOfFiles.length; i++) {
theNamesOfFiles[i] = filelist[i].getName();
}
The adapter to use with the list :
new ArrayAdapter<String>(this, android.R.layout.simple_list_item, theNamesOfFiles);
For anything more complicated than showing the names of the files you have to implement a custom adapter.
Or you can use something like this for a sorted String of filenames:
File dataDirectory = Environment.getDataDirectory();
File fileDir = new File(dataDirectory, "data/com.yourapp.app/files");
String[] listItems = fileDir.list();
Arrays.sort(listItems);