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);
Related
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 have several image files in my sdcard inside the folder req_images. I want to delete all the image files in this folder. So I wrote the following code. The arraylist f stores the list of all image files in it in the form /mnt/sdcard/req_images/Image-1.jpg. When I try to delete all the files one by one using the for loop, the files are not deleting from my sd card. The boolean result which I get as output was false while I try to delete all the files. The file is still there in the sd card and I have checked it. What is the possible error in it.
ArrayList<String> f = new ArrayList<String>(); // list of available files in path
File[] listFile;
public void getSdcardImages()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"req_images");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
}
}
}
for(int h=0;h<f.size();h++)
{
File fil=new File(f.get(h));
Boolean bool= fil.delete();
Log.d("File deletion status"+h,bool.toString());
}
Instead of doing it this way try a different approach
File dir = new File(Environment.getExternalStorageDirectory() + "/req_images");
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
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.
I would like to scan a specific folders for all filenames inside to save them to an array.
But how can I access the sd Card to do this?
Help would be great (io / enviroment???)
Environment.getExternalStorageDirectory() will link you to the SD card if one is inserted in the phone. Otherwise it just links you to your phone storage. Here's a link to more information on Environment http://developer.android.com/reference/android/os/Environment.html.
File file = new File(Environment.getExternalStorageDirectory()+"/folder/");
File[] files = file.listFiles();
String[] fileNames = new String[files.length];
for(int i = 0; i < files.length; i++)
fileNames[i] = files[i].getName();
You can use the following code:
String filesDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()+"/YOUR_SPECIFIC_FOLDER/"
File fileList = new FilefilesDirectory
if (fileList != null)
{
File[] filenames = fileList.listFiles();
//Loop through each file and get the file name
String fileNamesArray[] = new String[filenames.length];
int i=0;
for (File tempFile : filenames)
{
fileNamesArray[i] = tempFile.getName();
i++;
}
}
I have a folder called images in the sdcard of my emulator. This folder contains pictures clicked from my application. I want to display all the pictures from that folder to a listview. How can i do that? Thanks in advance.
This should get you started with it.
http://android-er.blogspot.com/2010/01/android-file-explorer-with-jpgs-exif_08.html
A simple logic to calculate the requirments,
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
I haven't tried it, but I suppose if you could be able to create IMageView by putting the images from SD card, you can be able to do this. You can get the images from SD card by creating ContentResolver cursor by passing MediaStore.Images.Media.EXTERNAL_CONTENT_URI and then creating imageview by passing the URI and id (from cursor) to it. After you create ImageView you can create an array of it and pass it to the ArrayAdapter which will then be used to set the data to your ListView. I hope this will help you to solve the problem.
ArrayList<File> imageReader(File root) {
ArrayList<File> a = new ArrayList<>();
File[] files = root.listFiles();
for(int i=0; i < files.length; i++) {
if(files[i].isDirectory()) {
a.addAll(imageReader(files[i]));
}
else {
if(files[i].getName().endsWith(".jpg")) {
a.add(files[i]);
}
}
}
return a;
}
give your root directory as argument, add all the extensions you want to retrieve.