Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you open multiple files with intent in Android?

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
    }
}
like image 453
Regnodulous Avatar asked Sep 08 '14 08:09

Regnodulous


People also ask

How can I open multiple files in android Studio?

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.

How do I select multiple files in android programmatically?

To select multiple files press on as many files as you want to select and check marks will appear over all of the selected files. OR you press the More options menu icon in the upper right corner of the screen and press Select.

Is intent deprecated?

The Intent is not deprecated the problem is with your itemClickable. class because it is not recognized.

What is the purpose of an Intent in android?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents.


1 Answers

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.

like image 165
ashu Avatar answered Sep 23 '22 01:09

ashu