how to pick few type of file via intent in android

2019-01-12 00:54发布

I am uploading selected file to server but i know i wanna restrick user to pick only document file(.doc , .pdf etc) and image file.

For now my code is working for all files it fetches uri of all files, So please tell me how to restrict user pick only specific type of file.

Here is my code to pick any file.

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

2条回答
唯我独甜
2楼-- · 2019-01-12 01:27

Pass multiple MIME types separate with | like

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

or create an array of MIME types like

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

and pass it as

i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
查看更多
不美不萌又怎样
3楼-- · 2019-01-12 01:48

Though not through an Intent but I have found a good library project by droidninja that enables one to browse through doc files or images stored locally in a single go.

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

Insert this in your app.gradle file

compile 'com.droidninja:filepicker:1.0.6'

then call this below given function to have a material themed dialog box which will give one an option to choose whether to choose an images or group or same with docs

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

For this dialog box though, you need to have in gradle file

compile com.github.javiersantos:MaterialStyledDialogs:2.0'

finally, onActivityResult() will be called to extract the result something like this

   @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>
查看更多
登录 后发表回答