【发布时间】:2012-03-29 10:42:33
【问题描述】:
如何使用intent提示用户选择“完成操作”来选择应用程序选择文件(假设设备中有几个应用程序浏览文件)
我想使用扩展名过滤文件..(例如:*.sav、*.props)
提前谢谢你
【问题讨论】:
-
访问这个线程,stackoverflow.com/questions/5537907/… 如果我没记错的话。这可能会对你有所帮助。
如何使用intent提示用户选择“完成操作”来选择应用程序选择文件(假设设备中有几个应用程序浏览文件)
我想使用扩展名过滤文件..(例如:*.sav、*.props)
提前谢谢你
【问题讨论】:
你可以这样使用:
....
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, YOUR_RESULT_CODE);
....
但是我真的怀疑你可以设置一个过滤器第三方文件浏览器。 或者您可以尝试使用此文件对话框:http://code.google.com/p/android-file-dialog/
【讨论】:
intent.setType("*/*")。我知道没有以file/ 开头的 MIME 类型。
这将打开内置文件资源管理器(如果可用),否则将要求您选择已安装的文件资源管理器。
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType("*/*"); //all files
intent.setType("text/xml"); //XML file only
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
【讨论】:
它可以帮助您选择一个 doc 文件,您可以根据需要更改操作
Intent intent;
if (VERSION.SDK_INT >= 19) {
intent = new Intent("android.intent.action.OPEN_DOCUMENT");
intent.setType("*/*");
} else {
PackageManager packageManager =getActivity().getPackageManager();
intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("file*//*");
if (packageManager.queryIntentActivities(intent,MEDIA_TYPE_IMAGE).size() == 0) {
UserToast.show(getActivity(), getResources().getString(R.string.no_file_manager_present));
}
}
if (getActivity().getPackageManager().resolveActivity(intent, NativeProtocol.MESSAGE_GET_ACCESS_TOKEN_REQUEST) != null) {
startActivityForResult(intent, UPLOAD_FILE);
}
【讨论】:
// check here to KitKat or new version and this will solve the Samsung file explore issue too.
boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat) {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,FILE_SELECT_CODE);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,FILE_SELECT_CODE);
}
【讨论】: