Adding all file paths from a folder to String array - android

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
}

Related

populate custom listview from audio files inside a folder

I'm trying to get files from a folder and populate recyclerview based on the name of files using a custom adapter.
This is how I'm doing it:
In onBindViewHolder:
Product m = dataList.get(position);
//title
holder.title.setText(m.getTitle());
And :
void popList() {
Product product = new Product();
File dir = new File(mainFolder);//path of files
File[] filelist = dir.listFiles();
String[] nameOfFiles = new String[filelist.length];
for (int i = 0; i < nameOfFiles.length; i++) {
nameOfFiles[i] = filelist[i].getName();
product.setTitle(nameOfFiles[i]);
}
songList.add(product);
}
But the problem is, it just adds the first item.
I can't figure it out where should I loop to add it all.
You need to create separate product objects for items in loop and add it to list instead of creating a single Product object in list which will hold the last set data
void popList() {
Product product ;
File dir = new File(mainFolder);//path of files
File[] filelist = dir.listFiles();
String[] nameOfFiles = new String[filelist.length];
for (int i = 0; i < nameOfFiles.length; i++) {
// create product
product = new Product();
nameOfFiles[i] = filelist[i].getName();
product.setTitle(nameOfFiles[i]);
// add it to list
songList.add(product);
}
}
Your code walk through
void popList() {
Product product = new Product(); // one object
// ..code
for (int i = 0; i < nameOfFiles.length; i++) {
nameOfFiles[i] = filelist[i].getName();
product.setTitle(nameOfFiles[i]); // at the end of loop set last file name to object
}
songList.add(product); // one object in the list , end of story
}

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

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
}

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 a list of filenames in a drawable resource directory

I want to get a list of certain filenames in a drawable resource directory (at runtime, not using XML). I guess, I would need to distinguish certain files from others. So ...
*res/drawable-hdpi/mysubdir
-- x-one.png
-- one.png
-- x-two.png
-- two.png
-- x-three.png
-- three.png
*
I want to put the x-*.png filenames into a List<String>. Is this possible?
Borrowing some code from another question, I can get all the drawables, but I don't see a way to distinguish one from another in a meaningful way.
private void getDrawableResources() {
final R.drawable drawableResources = new R.drawable();
final Class<R.drawable> drawableClass = R.drawable.class;
final Field[] fields = drawableClass.getDeclaredFields();
final List<Integer> resourceIdList = new ArrayList<Integer>();
for (int i = 0, max = fields.length; i < max; i++) {
final int resourceId;
try {
resourceId = fields[i].getInt(drawableResources);
resourceIdList.add(resourceId);
}
catch (Exception e) {
continue;
}
}
Resources resources = this.getActivity().getResources();
for (int i = 0; i < resourceIdList.size(); i++) {
Drawable drawable = resources.getDrawable(resourceIdList.get(i));
resourceIdList.get(i) + "): " + drawable.toString());
}
}
Resource directories do not support subdirectories.
Use FileNameFilter while getting list of files in a directory.
File dir = new File(dirLocation);
if(dir.exists() && dir.isDirectory())
{
File[] files = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.contains("X-");
}
});
}

Categories

Resources