简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM