您可以在Android中打开多个文件吗? [英] Can you open multiple files with intent in Android?

查看:165
本文介绍了您可以在Android中打开多个文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android Studio中编写一个可打开多个音乐文件并存储其路径的应用程序.目前,我正在做的是一次加载一个文件,而没有任何问题.例如,以下代码显示了我的onclicklister(用于加载按钮)和相关代码.此示例已简化了一些代码.用户单击应用程序中的加载"按钮,然后使用他们安装的任何文件管理器来选择文件,然后将Uri传递回我的应用程序.一切都很好.但是,是否可以选择多个文件并将它们作为文件数组传递回我的应用程序?因此,用户可以选择5或6,而不是选择单个音频文件.

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.

无论如何-我

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);

注意:上述方法仅适用于18级及以上的API.为了支持API级别< 18在您的应用中,使用一些库项目,例如 Android多文件选择器对话框

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.

这篇关于您可以在Android中打开多个文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆