我正在开发一个应用程序,其中用户从任何类型的文件管理器中选择一个文件,然后将其存储在数组中,然后在需要时打开此文件,但是问题是我尝试并搜索了很多,但我没有成功
这是我写的: 要获取路径:
final int PICKFILE_RESULT_CODE = 1001;
btnBrowse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "DEMO"),1001);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICKFILE_RESULT_CODE:
if (resultCode == RESULT_OK) {
Uri currFileURI = data.getData();
String path=currFileURI.getEncodedPath();
arrCoursesPath.add(path);
txtPath.setText(path);
}
break;
}
}
要打开文件:
private void openPath() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("file:"+arrCoursesPath.get(0));
intent.setDataAndType(mydir,"application/*"); // or use */*
startActivity(intent);
}
执行时,错误发生在打开文件时,它告诉我该元素不存在
解决方案,伙计们!!