How to show only non-hidden folders programmatically - android

So I have this simple method that helps list all of the files in a directory (hidden and non-hidden) and I wish to edit it to display only non hidden files.
public ArrayList<String> GetFiles(String DirectoryPath) {
ArrayList<String> MyFiles = new ArrayList<String>();
File file = new File(DirectoryPath);
//You create an array list
//The public final field length, which contains the number of components of the array
file.mkdirs();
File[] files = file.listFiles();
//list all of the files in the array
if (files.length == 0)
//if there are no files, return null
return null;
else {
//if the number of files is greater than 0, add the files and their names
for (int i = 0; i < files.length; i++)
MyFiles.add(files[i].getName());
}
return MyFiles;
How can I modify the above code to only display non hidden files?
By the way, inserting a . in front of a file name hides the file.
Thank you.

You should be able to make use of the isHidden(). Refer to the docs here.
//if the number of files is greater than 0, add the files and their names
for (int i = 0; i < files.length; i++) {
if(!files[i].isHidden())
MyFiles.add(files[i].getName()); // Add non hidden files
}

Related

List Files With Extensions Contained In Array

I would like to list image files from FTP with extensions defined in an array, I can check if an array contains a certain string but can't do the reverse of checking if part of a file (extension) is contained in an array, I tried:
String ftpFileName;
String[] extImage = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff"};
String[] listNames = client.listNames();
for (int i = 0; i < listNames.length; i++) {
ftpFileName = listNames[i];
if (ftpFileName.contains(Arrays.asList(extImage)))
{
//here I will handle the returned image file names
}
}
Seems like I can only do this Arrays.asList(extImage).contains(ftpFileName) but need the reverse of this since I'm only checking a "part" of the file name from array.

Adding all file paths from a folder to String array

I am trying to generate an array with each item in the array being the path of a video located in a folder named raw in resources. However the program crashes when it is run. Here is the code for adding the file names to a String array.
String[] fileArray;
File dir = new File("/res/raw");
File[] files = dir.listFiles();
fileArray = new String[files.length];
for (int i = 0; i < files.length; ++i){
fileArray[i] = files[i].getName();
}
You need this:
Field[] fields = R.raw.class.getFields();
for (int i = 0; i < fields.length - 1; i++) {
String name = fields[i].getName();
//do your thing here
}

How to read file names from Internal storage in Android

I want to read the file names from the default internal storage folder of my app but even if there are some files I get a List of size 0 I use the following to read the file names
File dirFiles = Settings.this.getFilesDir();
File list[] = dirFiles.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
What I am doing wrong here? is the path I get correct? or it needs to have a "/" at the end of it?
/data/data/com.compfrind.contacts/files
Try this way
private File path = new File("/storage/" + "");
File list[] = path.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
Also please have a look at this answer

get only last array vlaue from ArrayList android

I am using GridView with camera to show images (maximum 4 images)
i need all the captured images path in ArrayList ,
The problem is when the loop iterates , it give me previous array value along with latest array
Eg:
iteration 1 : [imagepath1]
iteration 2 : [imagepath1,imagepath2]
iteration 3 : [imagepath1,imagepath2,imagepath3]
I need only the latest iterated value
i.e : [imagepath1,imagepath2,imagepath3]
// ArrayList<String> imageList;
private List<String> RetriveCapturedImagePath() {
List<String> tFileList = new ArrayList<String>();
File f = new File(GridViewDemo_ImagePath);
if (f.exists()) {
File[] files = f.listFiles();
Arrays.sort(files);
//Log.e("f.listFiles();", "files "+files);
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory())
continue;
tFileList.add(file.getPath());
Log.e("file.getPath()", "karfile " + file.getPath());
imageList.add(file.getPath());
Log.e("imageList RetriveCapturedImagePath", "karimageList" + imageList);
}
}
return tFileList;
}

Get files and directory listings in android

I am working on an android project and I am trying to get a list of files and directories from the SD Card. It seems to be more a less working except the file name is outputting a load of nonsense and I can't see why.
Below is the code I am using to get the file listing.
public ArrayList getFileDirectoryListing()
{
ArrayList fileAndDirectories = new ArrayList();
final String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File[] files = Environment.getExternalStorageDirectory().listFiles();
for (int i = 0; i < files.length; i++)
{
FileDirectoryDetails fileDirectoryDetails = new FileDirectoryDetails();
fileDirectoryDetails.path = files[i].getName();
if (files[i].isDirectory())
{
fileDirectoryDetails.fileOrDirectory = FileOrDirectory.Directory;
}
else
{
fileDirectoryDetails.fileOrDirectory = FileOrDirectory.File;
}
fileAndDirectories.add(fileDirectoryDetails);
}
}
return fileAndDirectories;
}
Below is the code I am using to set the list adapter
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
listView = getListView();
ArrayList<FileDirectoryDetails> filesAndDirectories = getFileDirectoryListing();
fileDirectoryDetailsArrayAdapter = new
ArrayAdapter<FileDirectoryDetails>(this, android.R.layout.simple_list_item_1, filesAndDirectories);
setListAdapter(fileDirectoryDetailsArrayAdapter);
}
Below is a screenshot of what I am getting back in the list view instead of the actual file names.
Make sure you override toString() in your FileDirectoryDetails returning meaningful details. Currently you're using the default toString()
Or just fill your array with paths strings instead of the whole FileDirectoryDetails
Alternatively, override getView() of the adapter setting the text of the TextView to details.path

Categories

Resources