get only last array vlaue from ArrayList android - 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;
}

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
}

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 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
}

comparing between two arraylists and remove extra element from there

how do I compare two arraylists?
I have a sdCoverList array and thumbnailList array.
02-04 11:05:05.210: I/System.out(4035): HASHED THUMBNAIL[-2122410790, 2043473787, 1914391068, 1785308349, 1656225630, 1527142911, 1398060192, 1268977473, 1139894754, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549]
02-04 11:05:05.210: I/System.out(4035): SD COVER LIST[-2122410790, 2043473787, 1914391068, 1785308349, 1268977473, 1656225630, 1527142911, 1139894754, 1398060192, 1010812035, 1242943301, 1113860582, 984777863, 855695144, 726612425, 597529706, 468446987, 339364268, 210281549, 717409028]
In my sd cover list, i have an extra value that i want to remove away but first i'd have to compare with thumbnailList array first. if both have the same elements, don't do anything but if theres an extra, remove it from sd card.
My array lists
for sdCoverList:
// CHECK DIRECTORY BEFORE DELETING THEM
File strPath = new File(Environment.getExternalStorageDirectory() + folderName+"/Covers");
File yourDir = new File(strPath, "");
if(yourDir.length()!=0)
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
sdCoverList.add(name);
//System.out.println(sdCoverList);
}
for hashedthumbnail
for (int a=0;a<=thumbnailList.size()-1;a++)
{
String hashed = String.valueOf(thumbnailList.get(a).hashCode());
Log.d("hashed",hashed);
hashedThumbnail.add(hashed);
}
List<String> sdCoverList = new ArrayList<String>();
sdCoverList.add("-2122410790");
sdCoverList.add("2043473787");
sdCoverList.add("717409028");
List<String> hashedThumbnail = new ArrayList<String>();
hashedThumbnail.add("-2122410790");
hashedThumbnail.add("2043473787");
System.out.println("sdCover List: " + sdCoverList);
System.out.println("hashedThumbnail List: " + hashedThumbnail);
List<String> temp = new ArrayList<String>();
temp.addAll(sdCoverList);
temp.removeAll(hashedThumbnail);
System.out.println("temp List: " + temp);
sdCoverList.removeAll(temp);
System.out.println("sdCover List: " + sdCoverList);
The output will be
sdCover List: [-2122410790, 2043473787, 717409028]
hashedThumbnail List: [-2122410790, 2043473787]
temp List: [717409028]
sdCover List: [-2122410790, 2043473787]

display all files from specific folder from sdcard Android

how can i display all the files (.pdf, .doc, .ppt, .mp4, etc...) inside a folder in sdcard? i want to display their filenames.
here's what i've got:
File files = new File(Environment.getExternalStorageDirectory() + "/Downloads/Files/");
File[] list = files.listFiles();
int lists = files.listFiles().length;
String qwe = list.toString();
Log.d(LOG_TAG, "list: " + lists);
Log.d(LOG_TAG, "names: " + qwe + "\n"); //i want to show their filenames
for(int i=0; i < questionfiles.listFiles().length; i++){
if(list[i].isHidden()){
Log.d(LOG_TAG, "hidden path files.."+list[i].getAbsolutePath());
}
}
You have to use a ListActivity with an Adapter (ArrayAdapter for istance).
see this as example
EDIT: You can print the array content in this way:
Log.i(LOG_TAG, Arrays.toString(list));

Categories

Resources