5

I'm trying to write an app in Android Studio that opens multiple music files and stores their paths. At the moment all I'm doing is loading one file at a time which works without issue. For example - the code below shows my onclicklister for the load button and associated code. Some of the code has been simplified for this example. A user clicks the load button in the app and then uses whatever file manager they have installed to select a file which then passes the Uri back to my app. All well and good. However, is it possible to select multiple files and have them passed back to my app as an array of files? So instead of selecting a single audio file maybe the user selects 5 or 6.

If so how do you do this? Many thanks.

Anyway - what I

final View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
    public void onClick(final View v) {

        int resID2 = v.getId();

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/*");
        try {
            startActivityForResult(intent,resID2); }
        catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Please install a file manager",Toast.LENGTH_LONG).show();
        }
    }
};


public void onActivityResult(int requestCode, int resultCode, Intent result) {

    if (resultCode == RESULT_OK)
    {
        Uri data = result.getData();
        String thePath = data.getPath();
        // Do something with the file path
    }
}
Regnodulous
  • 473
  • 7
  • 22
  • Then the user should use a filemanager that supports multi select. And in onActivity result you should be aware of the specific content. I don't know if the filemanagers support this. Better make your own starting from an oopen source one. – greenapps Sep 08 '14 at 08:49
  • Hi thanks for the reply. ES File Explorer does support selecting multiple files. I'm just a bit unsure about how you handle the return. Is it an array of files? - has anyone done this before? Trying to avoid reinventing the wheel and writing a new file manager just to import a few files. Thanks – Regnodulous Sep 08 '14 at 08:52
  • Can you select one file with ES File Explorer? If yes then please show the used intent. I will inverstigate further then as this is interesting... Or is it just the posted code? Is the return different when you select more files? Tell what you know.. – greenapps Sep 08 '14 at 08:53
  • Hello again - apologies I think my last comment was a little misleading. Even though ES File Explorer does allow you to select multiple files when used independently it does not when called via an intent from my app. You just get to pick a single file and this gets returned as a Uri. I just wondered if I was missing a trick. Many thanks – Regnodulous Sep 08 '14 at 09:15
  • @Regnodulous : how about using some project like [afiledialog](http://code.google.com/p/afiledialog/)? – ashu Sep 08 '14 at 09:21
  • But your code does not let me even pick one file. What did you do to let it pick? – greenapps Sep 08 '14 at 09:22
  • http://www.estrongs.com/res/develop_en.htm Shows only how to pick one file. And it does not work here like that as `new Intent("com.estrongs.action.PICK_FILE ")`is alreaady very peculiar. – greenapps Sep 08 '14 at 09:37
  • @ashu where is documented that aFileDialog can pick more files? – greenapps Sep 08 '14 at 09:43
  • Hello greenapps - forgot to say you also need to add to your manifest and set your button to use the global onclick listener - button.setOnClickListener(mGlobal_OnClickListener); Otherwise the code should work ok. – Regnodulous Sep 08 '14 at 09:44
  • Happy to consider all options. Basically all I'm looking for is a way to select multiple audio files and import them to create a playlist. I just don't wan to have to write an entire file manager if I can use a built in app and a simple call to do the same job. Thanks again. – Regnodulous Sep 08 '14 at 09:46
  • That permissionis is complete notsense. As es file explorer is going to look somewhere. Not your app. Having said that you could change the subject of this post as we are talking about selecting files with an external app. Not about opening files. – greenapps Sep 08 '14 at 09:51
  • @Regnoludous : Sorry man! It appears that I mentioned wrong library in comments. The library I actually wanted you to tell about was [Android Multiple File Selector Dialog](https://github.com/paulasiimwe/Android-Multiple-file-Selector-Dialog). – ashu Sep 08 '14 at 09:59
  • Hello greenapps - sorry if the permission is incorrect. I think you still need it though if you want to do something with the file later. For example - in my case I store the filepath and then later use it to feed a file directly into mediaplayer. At which point its my app talking to the file directly - but I do get your point. Anyway - it seems that this is not as simple as it sounds. I'll have a look at Ashu's suggestion - maybe that's the easiest way round. Thanks everyone. – Regnodulous Sep 08 '14 at 10:05
  • 1
    What you want to do with the file later is irrelevant for the problem here. Here we were only discussing how to pick filenames. – greenapps Sep 08 '14 at 10:29
  • greenapps - yes I know. I was saying sorry for my mistake. You don't need to hammer the point home. – Regnodulous Sep 08 '14 at 12:42

1 Answers1

6

You will have to put an extra value to your intent inorder to create a chooser to pick multiple files.

Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);  
chooseFile.setType("audio/*");  
chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(chooseFile, "Choose a file") , 2);

Note: Above method will work only of API level 18 and above. To support API level < 18 in your app, use some library project like Android Multiple File Selector Dialog.

ashu
  • 1,756
  • 4
  • 22
  • 41
  • I've marked the answer above as correct although technically I found that no file explorer I tried supported the method. Maybe something to bear in mind for the future. I did look at the project linked above as well but couldn't work out how to use it in Android studio so if anyone else can point me to another project that might be better documented please feel free to do so. Many thanks – Regnodulous Sep 09 '14 at 14:46
  • @Regnodulous : See [this](http://stackoverflow.com/questions/16588064/how-do-i-add-a-library-project-to-the-android-studio) if you are having trouble importing the project in Android Studio. – ashu Sep 09 '14 at 14:59