如何通过意图在Android中选择几个类型的文件?

我上传选定的文件到服务器,但我知道我想restrick用户只选择文档文件(.doc,.pdf等)和图像文件。

现在我的代码正在为所有文件提取所有文件的所有文件,所以请告诉我如何限制用户只挑选特定类型的文件。

这是我的代码选择任何文件。

Intent i=new Intent();
                i.setType("*/*");
                i.setAction(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(Intent.createChooser(i, "abc"),requestCode); 

使用|分隔多个MIME类型 喜欢

i.setType("image/*|application/pdf|audio/*");

或者像创建一个MIME类型的数组

String[] mimetypes = {"image/*", "application/*|text/*"};

并通过它

i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);

虽然没有通过意图,但我已经找到了droidninja的一个好的图书馆项目,使人们能够一次浏览doc文件或本地存储的图像。

repositories {
    jcenter()
    maven {
        url "https://jitpack.io"
    }
}

插入你的app.gradle文件

compile 'com.droidninja:filepicker:1.0.6'

然后在下面的给定函数中调用这个对话框,该对话框将提供一个选项来选择是否选择图像或组或与文档相同

private void showFileChooser() {

    new MaterialStyledDialog.Builder(getActivity())
            .setTitle("Upload Documents")
            .setDescription("Upload single or multiple documents in a single attempt now, maximum being 5.n nChoose between Images option or PDF's option now. n")
            //.setStyle(Style.HEADER_WITH_ICON)
            .setHeaderColor(R.color.colorPrimary)
            .withDialogAnimation(true)
            .setIcon(R.drawable.ic_pdf)
            .setCancelable(true)
            .autoDismiss(false)
            .setPositiveText(R.string.images)
            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.dismiss();
                    FilePickerBuilder.getInstance().setMaxCount(5)
                            .setSelectedFiles(selectedPhotos)
                            .setActivityTheme(R.style.AppTheme)
                            .pickPhoto(getActivity());
                }
            })
            .setNeutralText(R.string.pdf)
            .onNeutral(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.dismiss();
                    FilePickerBuilder.getInstance().setMaxCount(5)
                            .setSelectedFiles(filePaths)
                            .setActivityTheme(R.style.AppTheme)
                            .pickDocument(getActivity());
                }
            })
            .show();
}

对于这个对话框,你需要在gradle文件中

compile com.github.javiersantos:MaterialStyledDialogs:2.0'

最后,将调用onActivityResult()来提取类似这样的结果

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FilePickerConst.REQUEST_CODE_PHOTO:
                if (resultCode == Activity.RESULT_OK && data != null) {
                    selectedPhotos = new ArrayList<>();
                    selectedPhotos.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS));
                }
                break;
            case FilePickerConst.REQUEST_CODE_DOC:
                if (resultCode == Activity.RESULT_OK && data != null) {
                    filePaths = new ArrayList<>();
                    filePaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS));
                }
                break;
        }

    }

AppTheme

  <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

        <!-- Customize your theme here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
链接地址: http://www.djcxy.com/p/45445.html

上一篇: how to pick few type of file via intent in android?

下一篇: view .doc, .docx, .rtf, .ppt file in iphone using Webview