List Files With Extensions Contained In Array - android

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.

Related

Array, with if statement

So i am busy with a program where i have this array called names and in the array i have the following data:
public static String [] name = {"Products", "Customers","Calculators","Logout"};
public static String [] subName = {"140 wide Plain", "140 wide Pattern", "280 wide Running width", "280 wide plain drops","280 wide pattern drops"};
now the following code i am trying to implement an if statement:
private List<TitleMenu> getList() {
List<TitleMenu> list = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
List<SubTitle> subTitles = new ArrayList<>();
if (names.equals("Calculators")){
for (int j = 0; j < subNames.length; j++) {
SubTitle subTitle = new SubTitle(subNames[j]);
subTitles.add(subTitle);
}}
TitleMenu model = new TitleMenu(names[i], subTitles, null);
list.add(model);
}
return list;
}
so i only want the second portion of my code to implement if the if statement is correct but i cant seem to get the if statement correct. I am coding in android studio so i dont know how my if statement needs to be
You need to reference the indices of the array. Each index in an array is represented by a number that starts at base zero. This is an important and fundamental concept across many programming languages.
It also looks like your array's name is not names and is name.
So you could check to see if the name is "Calculators" by doing something like this:
if (name[i].equals("Calculators")) { ... }

How to show only non-hidden folders programmatically

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
}

Fetch all songs from specific path

I am trying to fetch all songs from a specific folder. Currently, I am fetching all songs and then on basis of path I am looping and getting songs in the specific path.Any better way to do this?
for (int i = 0; i < totalSongList.size(); i++) {
String path = totalSongList.get(i).getPathId();
int index = path.lastIndexOf("/");
String folderPath1 = path.substring(0, index);
if (folderPath.equals(folderPath1))
songList.add(totalSongList.get(i));
}
I can't use below code also as it will fetch sub folder songs also.
musicResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%SPECIFIC_FOLDER_NAME%"}, null);
Here is java code without using mediaStore for retrieving all songs from the specific folder which may or maynot contain any subfolder.
public String File path = new File("your folder path here");
public void Search(File dir)
{
if(dir.isFile())
{
//get necessary songName over here
String songName = dir.getName();
//if you need path
String songPath = dir.getAbsolutePath();
//if your folder consists of other files other than audio you have to filer it
if(songName.endsWith(".mp3") || songName.endsWith(".MP3")
{
//this is the required mp3 files
//do what you want to do with it
}
}else if(dir.isDirectory())
{
File[] list = dir.listFiles();
for(int i=0;i<list.length();i++)
{
search(list[i]);
}
}
else
{
//handle exceptions here
}
}

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

Android: Sorting mp3 files by title in a clickable listview

I am attempting to create a music player app for a Nexus 7 tablet. I am able to retrieve music files from a specific directory as well as information that is associated with them such as title, artist, etc. I have loaded the files's titles into a clickable list view. When the user clicks on a title, it takes them to an activity that plays the associated song. I was attempting to sort the titles alphabetically and ran into a snag. When I sort just the titles, they no longer match with the correct songs. This is to be expected since I only ordered the titles and not the actual files. I attempted to modify the sorting algorithm by doing this:
//will probably only work for Nexus 7
private final static File fileList = new File("/storage/emulated/0/Music/");
private final static File fileNames[] = fileList.listFiles(); //get list of files
public static void sortFiles()
{
int j;
boolean flag = true;
File temp;
MediaMetadataRetriever titleMMR = new MediaMetadataRetriever();
MediaMetadataRetriever titleMMR2 = new MediaMetadataRetriever();
while(flag)
{
flag = false;
for(j = 0; j < fileNames.length - 1; j++)
{
titleMMR.setDataSource(fileNames[j].toString());
titleMMR2.setDataSource(fileNames[j+1].toString());
if(titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE).compareToIgnoreCase(titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)) > 0)
{
temp = fileNames[j];
fileNames[j] = fileNames[j+1]; // swapping
fileNames[j+1] = temp;
flag = true;
}//end if
}//end for
}//end while
}
This is supposed to retrieve the song titles from two files, compare them, and swap the files in the File array if the first comes after the second alphabetically. For some reason, when I run the activity that calls this method, the app crashes. If I remove the + 1 from titleMMR2's data source
titleMMR2.setDataSource(fileNames[j].toString());
the app no longer crashes but the list is not in order. Again this is understandable since it compares the song titles to themselves. I don't know why the + 1 would make the program crash. It is not an array out of bounds error. There are a total of 6 .mp3 files in the directory and they are the only files in that directory. I have also tried using Arrays.sort(fileNames) but that only orders them by their file name and not song title. I also tried this:
Arrays.sort(fileNames, new Comparator<File>(){
public int compare(File f1, File f2)
{
titleMMR.setDataSource(f1.toString());
titleMMR2.setDataSource(f2.toString());
return titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE).compareToIgnoreCase(titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
} });
That snippet also resulted in a crash. There are no errors in the java code and all appropriate classes have been imported. I'm really at a loss as to what is wrong. Any help will be appreciated and if any new info is needed I will gladly provide it. Thanks in advance.
FIXED
The correct code snip is this:
public static void sortFiles()
{
int j;
boolean flag = true;
File temp;
MediaMetadataRetriever titleMMR = new MediaMetadataRetriever();
MediaMetadataRetriever titleMMR2 = new MediaMetadataRetriever();
while(flag)
{
flag = false;
for(j = 0; j < fileNames.length - 1; j++)
{
titleMMR.setDataSource(fileNames[j].toString());
titleMMR2.setDataSource(fileNames[j+1].toString());
String title1;
String title2;
if(titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) == null)
title1 = fileNames[j].getName();
else
title1 = titleMMR.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
if(titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) == null)
title2 = fileNames[j+1].getName();
else
title2= titleMMR2.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
if(title1.compareToIgnoreCase(title2) > 0)
{
temp = fileNames[j];
fileNames[j] = fileNames[j+1]; // swapping
fileNames[j+1] = temp;
flag = true;
}//end if
}//end for
}//end while
}

Categories

Resources