文件首战插件刚刚推出的意图和其他应用程序打开文件,而不是你的应用程序,所以你不需要为PhoneGap的,只是看到插件代码,做同样的在你的Android项目。
有几个插件。 https://github.com/markeeftb/FileOpener
private void openFile(String url) throws IOException {
// Create URI
Uri uri = Uri.parse(url);
Intent intent = null;
// Check what kind of file you are trying to open, by comparing the url with extensions.
// When the if condition is matched, plugin sets the correct intent (mime) type,
// so Android knew what application to use to open the file
if (url.contains(".doc") || url.contains(".docx")) {
// Word document
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/msword");
} else if(url.contains(".pdf")) {
// PDF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
} else if(url.contains(".ppt") || url.contains(".pptx")) {
// Powerpoint file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if(url.contains(".xls") || url.contains(".xlsx")) {
// Excel file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if(url.contains(".rtf")) {
// RTF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/rtf");
} else if(url.contains(".wav")) {
// WAV audio file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "audio/x-wav");
} else if(url.contains(".gif")) {
// GIF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/gif");
} else if(url.contains(".jpg") || url.contains(".jpeg")) {
// JPG file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpeg");
} else if(url.contains(".png")) {
// PNG file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/png");
} else if(url.contains(".txt")) {
// Text file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/plain");
} else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
// Video files
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
}
//if you want you can also define the intent type for any other file
//additionally use else clause below, to manage other unknown extensions
//in this case, Android will show all applications installed on the device
//so you can choose which application to use
else { intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
}
this.cordova.getActivity().startActivity(intent);
}
只是改变this.cordova.getActivity()
你得到你在哪里调用代码的类活动的方式。
感谢您的回复。你能告诉我,如何将phonegap与非phonegap应用程序集成? – SKK 2014-12-05 12:09:58
phonegap应用程序是一些html/css/javascript文件。把这些文件放在资产文件夹中,然后打开一个'WebView'后,告诉你要加载你的'index.html'(phonegap的主文件),现在你已经完成了。 – 2014-12-05 12:18:36
我在示例phonegap应用程序中看到了一些文件夹,如CordovaLib/cordova/platform_www /文件夹。我是否也需要添加这些文件夹? – SKK 2014-12-05 12:26:21