ArrayList<File> contains filename - android

I have a problem with an ArrayList (File in the above code). This arraylist is composed by files that are located into the sd. The problem is that I can have duplicates (the same image, but in different paths into the sd, so the same filename but different path) and I want to remove them. So I use this code:
ArrayList<File> removedDuplicates = new ArrayList<File>();
for (int i = 0; i < File.size(); i++) {
if (!removedDuplicates.contains(File.get(i))) {
removedDuplicates.add(File.get(i));
}
}
But it doesn't work, I guess because contains() for a List of File looks at the filepath instead of at the filename. Is it true? How can I solve my problem? I also tried with:
ArrayList<File> removedDuplicates = new ArrayList<File>();
for (int i = 0; i < File.size(); i++) {
if (!removedDuplicates.contains(File.get(i).getName())) {
removedDuplicates.add(File.get(i));
}
}
but still it doesn't work. Thanks

The type of getName is String and the type of object in your ArrayList is File, so you're never going to get the same thing.
You want to compare the names inside the ArrayList.
for(File f : files){
String fName = f.getName();
boolean found = false;
for(File f2 : removedDuplicates){
if(f2.getName().equals(fName)){
found = true;
break;
}
}
if(!found){
removedDuplicates.add(f);
}
}

Its very simple.
PS: Tested Code
Map<String, File> removedDuplicatesMap = new HashMap<String, File>();
for (int i = 0; i < files.size(); i++) {
String filePath = files.get(i).getAbsolutePath();
String filename = filePath.substring(filePath.lastIndexOf(System
.getProperty("file.separator")));
removedDuplicatesMap.put(filename, files.get(i));
}
ArrayList<File> removedDuplicates = new ArrayList<File>(
removedDuplicatesMap.values());

Try this:
ArrayList<File> removedDuplicates = new ArrayList<File>();
File temp;
for (int i = 0; i < File.size(); i++) {
temp=new File(File.get(i).getAbsolutePath());
if (!removedDuplicates.contains(temp)) {
removedDuplicates.add(File.get(i));
}
}

Related

How to get all file list in my storage?

I'm making application that find specific file in my storage.
So I put all file list into List.
Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));
void searchFile(File directory){
File[] files = directory.listFiles();
try{
if(directory.exists()) {
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].exists()) {
if (files[i].isDirectory()) {
File[] file = files[i].listFiles();
for (int j = 0; j < file.length; j++) {
searchFile(file[j].getPath());
}
} else
fileList.add(files[i].getPath());
}
}
}
} catch(Exception ex){}
}
But My the number of all file is more than 60000.
So When I tried to debug, It worked so slowly.
How can I get All file list in my storage quickly?
Please try the below code.
Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));
void searchFile(File directory){
try{
if(directory.exists()) {
File[] files = directory.listFiles();
for (File file : files) {
if(file.exists()) {
if (file.isDirectory()) {
File[] innerFiles = file.listFiles();
for (File innerFile : innerFiles) {
searchFile(innerile.getPath());
}
} else
fileList.add(file.getPath());
}
}
}
} catch(Exception ex){}
}
This will narrow down your performance issue to a certain level.

Android crashes when looping through a string array [Android Studio]

I am trying to filter out specific strings in an array with the following code, but it keeps crashes when I try to run it.
public String[] lister(String[] fl){
String[] filelist = {};
int j = 0;
for(int i = 0; i < fl.length; i++){
if (fl[i].contains("Incident ")){
filelist[j] = fl[i];
j++;
}
}
return filelist;
}
I already tried some things, and it seems to be that
filelist[j] = fl[i];
is the culprit.
When I run
public String[] lister(String[] fl){
String[] filelist = {};
int j = 0;
for(int i = 0; i < fl.length; i++){
if (fl[i].contains("Incident ")){
//filelist[j] = fl[i];
j++;
}
}
filelist = fl;
return filelist;
}
It doesn't crash, even though it runs the forloop. Obviously the result is not what I need.
I don't understand why it does this? For some reason it feels as if it doesn't see 'filelist' initialized in the forloop.
Instead of array use ArrayList.
List<String> filelist = new ArrayList();
for(int i = 0; i < fl.length; i++){
if (fl[i].contains("Incident ")){
filelist.add(fl[i]);
}
}
return filelist;
Not possible to declare an array of unknown length, arrays are constant in length. Its better to use java.util.List implementation like ArrayList or List.
Or give your array a size like
String[] filelist = new String[3];
Solution 1: Using ArrayList
public String[] lister(String[] fl){
List<String> filelist = new ArrayList();
for(int i = 0; i < fl.length; i++){
if (fl[i].contains("Incident ")){
filelist.add(fl[i]);
}
}
return filelist;
}
Solution 2: Giving array a fixed length (Useful only if you know the length already)
public String[] lister(String[] fl){
String[] filelist = new String[3]; // or any size
for(int i = 0; i < fl.length; i++){
if (fl[i].contains("Incident ")){
filelist[i] = fl[i];
}
}
return filelist;
}
Note: Variable j is not really required
That's because the length of the variable filelist is 0, as you give no values {}, so you are getting ArrayIndexOutOfBoundsException as filelist[j] will always exceed the array bounds whatever the value of j is, so you have to define it like the following:
String[] filelist = new String[fl.length];

Get Image from sdcard using Prefix?

I am using json to get the image from url and successfully stored into sdcard.
I want to get image from sdcard and display into gridview. when I am using below code I can easily get the all image and easily display into grid view.
public void getFromSdcard() {
ArrayList<String> f = new ArrayList<String>();
File file = new File("/mnt/sdcard/IMG");
if (file.isDirectory()) {
listFile = file.listFiles();
Log.v("Length", "IMG ::" + listFile.length);
for (int i = 0; i < listFile.length; i++) {
f.add(listFile[i].getAbsolutePath());
}
}
}
The problem is I don't want to display all the images into grid view, i want to display specific images (using image prefix).
for example i have list of jpg below
LR-001.jpg
LR-002.jpg
GR-001.jpg
GR-002.jpg
CF-001.jpg
CF-002.jpg
ER-001.jpg
ER-002.jpg
GBR-001.jpg
GBR-002.jpg
GPT-001.jpg
GPT-002.jpg
NCK-001.jpg
NCK-002.jpg
how to get image of GR prefix?
You could check if the filename starts with "GR" for instance:
for (int i = 0; i < listFile.length; i++) {
String fileName = listFile[i].getName();
if (fileName.startsWith("GR"))
f.add(listFile[i].getAbsolutePath());
}
the same with the for-each
for (File file : listFile) {
String fileName = file.getName();
if (fileName.startsWith("GR"))
f.add(file);
}
to get a random entry from your ArrayList you can use the class Random
private Random randomGenerator;
randomGenerator = new Random();
int index = randomGenerator.nextInt(f.size());
File file = f.get(index);
nexInt returns a number between 0 and f.size() - 1
You could check your filenames for the starting letters "GR"
Following Code should work
List<File> f = new ArrayList<File>();
File file = new File("/mnt/sdcard/IMG");
if (file.isDirectory()) {
listFile = file.listFiles();
Log.v("Length", "IMG ::" + listFile.length);
for (File file : listFile) {
if(file.getName().startsWith("GR"){
f.add(file.getAbsolutePath());
}
}
}
Hope it will help you
You could use a FilenameFilter:
f = file.list(new FilenameFilter(){
#Override
public boolean accept(File dir, String filename){
if (filename.startsWith("GR")) return true;
return false;
}
});

Filtering files in a directory on Android

In my app i am getting the images from a folder in gallery and saving it into an array list.Now i want to extract only the files with .jpg extension.How can i do it
The code for saving to array list is
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
tFileList.add(file.getPath());
}
return tFileList;
}
You can use FilenameFilter interface to Filter the Files.
Change your codeline
File[] files=f.listFiles();
as below:
File[] jpgfiles = f.listFiles(new FileFilter() {
#Override
public boolean accept(File file)
{
return (file.getPath().endsWith(".jpg")||file.getPath().endsWith(".jpeg"));
}
});
Use .endsWith() method from Java String Class to check File Extension from file path.
Method:
public boolean endsWith(String suffix)
Your Code something like,
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
String filePath = file.getPath();
if(filePath.endsWith(".jpg")) // Condition to check .jpg file extension
tFileList.add(filePath);
}
return tFileList;
}
private List<String> ReadSDCard()
{
String extension = "";
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
int ind = files[i].getPath().lastIndexOf('.');
if (ind > 0) {
extension = files[i].getPath().substring(i+1);// this is the extension
if(extension.equals("jpg"))
{
tFileList.add(file.getPath());
}
}
}
return tFileList;
}
In your code add the following:
String filePath = file.getPath();
if(filePath.endsWith(".jpg"))
tFileList.add(filePath);

Accessing Android DCIM Images

How does one obtain images from the /DCIM/100ANDRO folder?
I have tried
File rootsd = Environment.getExternalStorageDirectory();
File dcim = new File(rootsd.getAbsolutePath() + "/DCIM/100ANDRO");
File[] imagelist = dcim.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
but I got a Null Pointer Exception.
Have you remembered to give the app permission to write the external memory?
It is done in the AndroidManifest.xml file, by adding
"android.permission.WRITE_EXTERNAL_STORAGE"
Since you are making a new file object, this is needed, I would guess.

Categories

Resources