0

I'm trying to list files from two different directories in an Android device in order to upload them to a server.

To do that, I'm getting the directory files under a File type and then throwing both listFiles() into correspondents File[] types.

    String d1 = Environment.getExternalStorageDirectory().getPath()+"/Download/";
    String d2 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera";

    File DirDocuments = new File(d1);
    File DirCamera = new File(d2);

    File[] ListDocuments = DirDocuments.listFiles();
    File[] ListCamera = DirCamera.listFiles();

I've tryed to merge then and uploadign using just a single "for" loop

    List<String> listTemp = new ArrayList<String>();

    for( int i=0; i< ListaDocuments.length; i++)
    {
            listTemp.add( ListaDocuments[i].getName() );
    }
    for( int i=0; i< ListaCamera.length; i++)
    {
            listTemp.add( ListaCamera[i].getName() );
    }

    File[] DirTotal = (String[]) listTemp.toArray(); //toArray gives Object[] and cannot be converted to File[], so I used String[]

But my app just stop and exits. What am I doing wrong?

Do anyone knows how can I merge ListDocuments and ListCamera into a single File[]?

Thanks in advance. (sorry about my typos and bad english)

5 Answers5

3

You could join both arrays' contents into a List<File> and not a List<String>:

    List<File> listTemp = new ArrayList<File>();

    for( int i=0; i< ListaDocuments.length; i++) {
            listTemp.add( ListaDocuments[i] );
    }
    for( int i=0; i< ListaCamera.length; i++)
    {
            listTemp.add( ListaCamera[i] );
    }

Notice there are other ways of joining those arrays (i.e. Apache Commons Collections ArrayUtils.addAll(T[], T...) as explained in How to concatenate two arrays in Java? and numerous duplicates).

And then just use the other version of toArray():

File[] DirTotal = listTemp.toArray(new File[0]);

toArray() without arguments will always return an Object[]. The version that takes an argument will fill up the passed array if it's big enough, and return a newly allocated array in case there's not enough room. The returned array is of the same type than the passed one, so if you pass a File[], you get a File[].

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Neither add or addAll are working. I've got the error:- The method addAll(Collection extends File>) in the type List is not applicable for the arguments (File[]) – SpaceVulcan Jan 28 '14 at 13:32
  • Thanks for this usefull suggestion, anyway. Due to the comments, I need to study the arrays subject further and deeply. If you have more ideas, please let me know. – SpaceVulcan Jan 28 '14 at 13:35
  • It seems you're using `addAll()` method defined in [`Collection`](http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html). `ArrayUtils.addAll()` belongs to a third-party library called [Apache Commons Collections](http://commons.apache.org/proper/commons-collections/) (you need to download it and place it on your classpath in order to use it). `listTemp.add(ListDocuments[i])` should be working fine, tho. `ListDocuments` is a `File[]`, so `ListDocuments[i]` is a `File`. It is fine as long as the `List` you're adding the elements to is a `List`, isn't it? – Xavi López Jan 28 '14 at 14:01
  • It seems that the libraries that comes with Android SDK are somehow slightly different than the "usual". I solved this with `files.addAll ( Arrays.asList( file.listFiles() ) );`. – SpaceVulcan Jan 29 '14 at 16:03
  • The entire function is `public List listFiles (String... paths) { List files = new ArrayList(); for ( String path : paths ) { File file = new File(path); File[] currentFileList = file.listFiles(); files.addAll ( Arrays.asList( file.listFiles() ) ); } return files; }`. I'm sending the two directories as parameters `String d1 = Environment.getExternalStorageDirectory().getPath()+"/Download/"; String d2 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/";` – SpaceVulcan Jan 29 '14 at 16:07
  • the function call is `List listTemp = new ArrayList(); listTemp = listFiles(d1,d2);` – SpaceVulcan Jan 29 '14 at 16:08
  • @SpaceVulcan Glad you solved it :-) Remember to upvote helpful answers and mark as accepted the one that solved your problem. If none of them qualifies (it looks like it didn't get through in your last comment), remember you can post your own answer and accept it. It will make it easier for future visitors of the question to identify a solution to the problem, and will also mark this question as resolved. – Xavi López Jan 29 '14 at 16:08
  • Thanks Xavi! I still don't have enough reputation to vote, but I choosed your answer as the solution. – SpaceVulcan Jan 29 '14 at 17:48
0

The problem is that the last line is casting to String array but the variable is defined as a File array.

jush
  • 613
  • 5
  • 13
0

Use this

 Object[] DirTotal =  listTemp.toArray();

But why you can't use this temp list?

Artem
  • 1,566
  • 1
  • 10
  • 15
0
public static void mergeFiles(File[] files, File mergedFile) {

    FileWriter fstream = null;
    BufferedWriter out = null;
    try {
        fstream = new FileWriter(mergedFile, true);
         out = new BufferedWriter(fstream);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    for (File f : files) {
        System.out.println("merging: " + f.getName());
        FileInputStream fis;
        try {
            fis = new FileInputStream(f);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));

            String aLine;
            while ((aLine = in.readLine()) != null) {
                out.write(aLine);
                out.newLine();
            }

            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

        String sourceFile1Path = "/mnt/sdcard/s1/t1.txt";
String sourceFile2Path = "/mnt/sdcard/s2/t2.txt";

String mergedFilePath = "/mnt/sdcard/merge/t.txt";

File[] files = new File[2];
files[0] = new File(sourceFile1Path);
files[1] = new File(sourceFile2Path);

File mergedFile = new File(mergedFilePath);

mergeFiles(files, mergedFile);
Dakshesh Khatri
  • 639
  • 7
  • 12
-2

you can use addAll() method in which is available in java.util package.

Pulah Nandha
  • 774
  • 4
  • 23