简体   繁体   English

Android打开文件从我的应用程序转换为合适的应用程序安装错误

[英]Android open file from my application into suitable application installed error

I have the following code to launch application chooser to open any type of file, but when ever I try to open the file I get the message file cannot be opened. 我有以下代码来启动应用程序选择器以打开任何类型的文件,但是无论何时我尝试打开文件,我都会收到无法打开消息文件的信息。

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(path));

if(type.contains("image")) {
    intent.setDataAndType(Uri.parse(path),type);
}else if(extension.equalsIgnoreCase("txt")){
    intent.setDataAndType(Uri.parse(path),"text/plain");
}else{
    intent.setDataAndType(Uri.parse(path),type);
}                       

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intentOpen = Intent.createChooser(intent, "Open File");

try {
  activity.startActivity(intentOpen);
} catch (ActivityNotFoundException e) {                  
}

any help will be appreciated, thanks in advance 任何帮助将不胜感激,在此先感谢

我解决了它,我必须在路径的开头添加file://来打开文件。

path="file://"+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOA‌​DS).getPath()+"/"+myFileName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM