我用科尔多瓦的代码打开文件,
var customPlugin = {
createEvent : function(success, failure, action,arg1) {
cordova.exec(success, // success callback function
failure, // error callback function
'Utils', // mapped to our native Java class
action, // with this action name
[arg1]);
}
}
这里的“Utils”指的是我的Java类,我在其中编写了下面提到的openFile函数,
// open the file
private boolean openFile(String fileName, String contentType)
throws JSONException {
File file = new File(fileName);
if (file.exists()) {
try {
Uri path = Uri.fromFile(file);
PackageManager packageManager = this.cordova.getActivity()
.getPackageManager();
String application_id = "com.processdrive.novema";
try {
PackageInfo packageInfo = packageManager.getPackageInfo(
this.cordova.getActivity().getPackageName(), 0);
application_id = packageInfo.packageName;
} catch (NameNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (Build.VERSION.SDK_INT >= 24) {
path = FileProvider.getUriForFile(this.cordova.getActivity(),
application_id + ".provider",
file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(path, contentType);
cordova.getActivity().startActivity(intent);
return true;
} catch (android.content.ActivityNotFoundException e) {
return false;
}
} else {
return false;
}
}
上面的代码在低于android 7的版本中可以正常工作,但是在高于android 7的版本中,它不能处理PDF文件中编辑的内容。