文件首戰插件剛剛推出的意圖和其他應用程序打開文件,而不是你的應用程序,所以你不需要爲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