Hal*_*alo 30 android android-intent
我想从存储中仅选择pdf,xlsx和txt文件,但intent.setType只能执行一个文件(仅限eg.txt文件(或)pdf文件).是否可以通过编码intent.setType()来获取所有三个文件,并且有办法吗?
这是我的一些代码.
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select txt file"),
0);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
}
}
Run Code Online (Sandbox Code Playgroud)
Zia*_*Zia 39
@Fatehali Asamadi的方式还可以,但需要添加一些适当的用途.对于Microsoft文档,使用(.doc或.docx),(.pt或.pptx),(.xls或.xlsx)扩展名.要支持或浏览这些扩展,您需要添加更多mimeTypes.
使用以下方法浏览REQUEST_CODE_DOC为onActivityResult(final int requestCode,final int resultCode,final Intent data)@Override方法的requestCode的文档.
private void browseDocuments(){
String[] mimeTypes =
{"application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
"application/vnd.ms-powerpoint","application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
"application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"text/plain",
"application/pdf",
"application/zip"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
}
startActivityForResult(Intent.createChooser(intent,"ChooseFile"), REQUEST_CODE_DOC);
}
Run Code Online (Sandbox Code Playgroud)
您可以获得清晰的概念并从此处添加所需的mimeTypes
Ris*_*hav 15
intent.setType("image/*|application/pdf|audio/*");
Run Code Online (Sandbox Code Playgroud)
也许这就是你想要的.
您应该检查此链接http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html。另请阅读 Mime Type。
这就是我为任何文件或选定的 MIME 类型文件实施的方式
String[] mimeTypes =
{"image/*","application/pdf","application/msword","application/vnd.ms-powerpoint","application/vnd.ms-excel","text/plain"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
}
startActivityForResult(Intent.createChooser(intent,"ChooseFile"), 0);
Run Code Online (Sandbox Code Playgroud)
看起来你只想知道这些意图是否可以解决.这可能是一种更好的方法:
private void showFileChooser() {
Intent intentPDF = new Intent(Intent.ACTION_GET_CONTENT);
intentPDF.setType("application/pdf");
intentPDF.addCategory(Intent.CATEGORY_OPENABLE);
Intent intentTxt = new Intent(Intent.ACTION_GET_CONTENT);
intentTxt.setType("text/plain");
intentTxt.addCategory(Intent.CATEGORY_OPENABLE);
Intent intentXls = new Intent(Intent.ACTION_GET_CONTENT);
intentXls.setType("application/x-excel");
intentXls.addCategory(Intent.CATEGORY_OPENABLE);
PackageManager packageManager = getPackageManager();
List activitiesPDF = packageManager.queryIntentActivities(intentPDF,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafePDF = activitiesPDF.size() > 0;
List activitiesTxt = packageManager.queryIntentActivities(intentTxt,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafeTxt = activitiesTxt.size() > 0;
List activitiesXls = packageManager.queryIntentActivities(intentXls,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafeXls = activitiesXls.size() > 0;
if (!isIntentSafePDF || !isIntentSafeTxt || !isIntentSafeXls){
// Potentially direct the user to the Market with a Dialog
}
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
http://developer.android.com/training/basics/intents/sending.html
小智 6
简单的解决方案是
Intent gallery=new Intent();
gallery.setType("application/*");
gallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(gallery,pick);
Run Code Online (Sandbox Code Playgroud)
注意:pick 是 int 变量。
您可以使用Intent.ACTION_OPEN_DOCUMENT,
每个文档都表示为由 a 支持的 content:// URI DocumentsProvider,可以使用 来将其作为流打开openFileDescriptor(Uri, String),或查询DocumentsContract.Document元数据。
所有选定的文档都将返回到调用应用程序,并授予持久的读写权限。如果您想在设备重新启动后保持对文档的访问,则需要使用 显式获取持久权限takePersistableUriPermission(Uri, int)。
调用者必须通过 指示可接受的文档 MIME 类型setType(String)。例如,要选择照片,请使用image/*。如果可以接受多个不相交的 MIME 类型,请在EXTRA_MIME_TYPES和setType(String)to中定义它们*/*。
欲了解更多详情,请参阅此链接
http://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT
请注意,这仅适用于 API 级别 19+。
| 归档时间: |
|
| 查看次数: |
44850 次 |
| 最近记录: |