public void openSystemFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "请选择文件!"),1);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "请安装文件管理器", Toast.LENGTH_SHORT).show();
}
}
选择完毕后会在onActivityResult(int requestCode, int resultCode, Intent data) 中获得文件的uri
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
}}
打开系统文件的方法
本文介绍了一个使用Android系统Intent来打开并选择本地文件的方法。通过创建Intent并设置类型为*/*,允许用户从设备上选择任意类型的文件。如果设备上没有安装文件管理器应用,则会提示用户安装。

被折叠的 条评论
为什么被折叠?



