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
Related
I have dynamic listView linked with JSONArray, in the adapter i'm trying to read the storage to check if the pdf file is downloaded already the Button text will be View and will call View method but if the the file not there the Button text will be Download and will call Download method.
I have been trying this code but it's not work
String path2 = Environment.getExternalStorageDirectory() + "/materialpdffolder/";
File path = new File(path2);
File list[] = path.listFiles();
for( int i=0; i < materialList.size() ; i++)
{
for( int x=0; x <= list.length ; x++) {
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
}
The problem is when there’s no file, the application is stack and give error and not give any results for list[x].get Name() and when I use materiallist.size() for for looping it is doesn’tgive a correct answer
Try to change with this code
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
Remove one bracket from }}else and put it end of else
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
}
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
}
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;
}
I want to get a time of all files that are in this folder ("/sdcard/Files/"). And then I want to delete all the files that have more than one hour.
Is there any method to do it?
File dir = new File("/sdcard/Files/");
File[] files = dir.listFiles();
for (int i = 0; i < files.length; ++i){
long lastTime = files[i].lastModified();
Date nowDate = new Date();
long nowTime = nowDate.getTime();
if (nowTime - lastTime > 60*60*1000){
files[i].delete();
}
}
I hope it can help you.