在Android中通过intent限制可选择的文件类型
我正在将所选文件上载到服务器,但我想限制用户仅选择文档文件(.doc、.pdf等)和图像文件 目前,我的代码适用于所有文件,它获取所有文件的uri 如何限制用户仅选择特定类型的文件 这是我选择任何文件的代码在Android中通过intent限制可选择的文件类型,android,file,android-intent,Android,File,Android Intent,我正在将所选文件上载到服务器,但我想限制用户仅选择文档文件(.doc、.pdf等)和图像文件 目前,我的代码适用于所有文件,它获取所有文件的uri 如何限制用户仅选择特定类型的文件 这是我选择任何文件的代码 Intent i=new Intent(); i.setType("*/*"); i.setAction(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(I
Intent i=new Intent();
i.setType("*/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(i, "abc"),requestCode);
使用
|
像
或者创建一个MIME类型数组,如
String[] mimetypes = {"image/*", "application/*|text/*"};
并将其作为
i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
虽然不是通过意图,但我发现droidninja有一个很好的库项目,它可以让人一次浏览本地存储的文档文件或图像
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
将此文件插入app.gradle文件
然后调用下面给定的函数来创建一个以材质为主题的对话框,该对话框将为用户提供一个选项来选择是选择图像还是组,还是选择与文档相同的内容
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文件中包含
最后,将调用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;
}
}
@覆盖
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例文件PickerConst.REQUEST\u代码\u照片:
if(resultCode==Activity.RESULT\u OK&&data!=null){
selectedPhotos=新建ArrayList();
selectedPhotos.addAll(data.getStringArrayListExtra(FilePickerConst.KEY\u SELECTED\u PHOTOS));
}
打破
案例文件pickerconst.REQUEST\u CODE\u DOC:
if(resultCode==Activity.RESULT\u OK&&data!=null){
filepath=newarraylist();
addAll(data.getStringArrayListExtra(FilePickerConst.KEY\u SELECTED\u DOCS));
}
打破
}
}
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>
真的
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
基于上述答案和类似的思路,我开发了一个有效的解决方案。我正在分享代码片段,所以它很容易使用
如何使用意图筛选或选择特定文件类型的文件。
代码
示例文件类型常量
用法
我所做的是将类型设置为
imageIntent.setType(“image/*\”,\“application/pdf”)代码>如何获取所选文件(pdf,文本)的路径你好,我曾尝试选择文件的doc或docx类型,但无法选择它。我的意图类型如下,intent.setType(“image/*| application/pdf | application/docm | application/docx | application/dot | application/mcw | application/rtf”+“| application/pages | application/odt | application/ott”);将mime类型添加到字符串数组中。这是mime类型的列表。这可能会对您有所帮助。@MadhukarHebbar我只想选择图像和视频。但当我打开文件管理器时,您甚至可以选择音频。它们不会被过滤掉。i.setType(“image/*| video/*”)
。既然文件管理器可以处理操作获取内容,我如何过滤音频和其他类型?第一种方法,即用|分隔,对我不起作用,但第二种方法使用字符串数组有效!此解决方案效果很好
compile com.github.javiersantos:MaterialStyledDialogs:2.0'
@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;
}
}
<!-- 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>
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);