我将所选文件上传到服务器,但我知道我想让restrick用户只选择文档文件(.doc,.pdf等)和图像文件。
现在我的代码适用于所有提取uri文件的文件,所以请告诉我如何限制用户只选择特定类型的文件。
这是我选择任何文件的代码。
Intent i=new Intent();
i.setType("*/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(i, "abc"),requestCode);
答案 0 :(得分:17)
将多个MIME类型与|
分开
像
i.setType("image/*|application/pdf|audio/*");
或创建一个MIME类型数组,如
String[] mimetypes = {"image/*", "application/*|text/*"};
并将其作为
传递i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
答案 1 :(得分:0)
虽然不是通过Intent,但我找到了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>
答案 2 :(得分:0)
基于上述答案和类似的线索,我开发了一个可行的解决方案。我正在共享代码段,因此易于使用。
如何使用意图针对特定文件类型过滤或选择文件。
代码
public static Intent getCustomFileChooserIntent(String ...types){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// Filter to only show results that can be "opened"
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, types);
return intent;
}
示例文件类型常量
public static final String DOC = "application/msword";
public static final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
public static final String IMAGE = "image/*";
public static final String AUDIO = "audio/*";
public static final String TEXT = "text/*";
public static final String PDF = "application/pdf";
public static final String XLS = "application/vnd.ms-excel";
用法
// i passed string values, you can pass a array of string too.
Intent intent = getCustomFileChooserIntent(DOC, PDF, IMAGE);
startActivityForResult(intent, 101);
答案 3 :(得分:0)
我所做的是将 setType 设置为
imageIntent.setType("image/*\", \"application/pdf");