cordova-plugin-file-opener2插件安卓第一次使用第三方应用打开文件同意权限后打开失败
cordova-plugin-file-opener2插件android使用第三方应用打开文件:第一次使用这个应用打开时会提示获取权限,允许后跳转到第三方,但是提示无法打开,回去自己应用再使用同一个应用打开就可以了。
想要第一次授权之后就能成功打开。谢谢!
路径什么的应该没问题,第二次就能打开了,感觉是不是出在了授权过程中,打开第三方应用的时候丢了文件路径。
第一次上传到邮箱的时候:提示的是文件不存在!
代码基本来自网上大佬。
// desc: 文件打开方法
function preview(fileEntry, fileName, resolve, reject) {
// 调用cordova-plugin-file-opener2插件实现用第三方app打开文件
try {
// cordova.plugins.fileOpener2.showOpenWithDialog(
cordova.plugins.fileOpener2.open(
// 此处必须填写cdvfile://地址,不然android7.0+会报文件权限错误
//fileEntry.toInternalURL(), //文件本地地址转cdvfile://地址
fileEntry.toURL(), //文件本地地址转cdvfile://地址
fileType[fileName.split('.')[1]],
{
error: function ( da ) {
console.log(
'出错!', error
)
reject( { Status: 'Error', Msg: '文件打开失败!', Error: error } )
},
success: function ( data ) {
console.log( '成功预览:', data )
resolve( { Status: 'Success', Msg: '文件打开Success!', data: data } )
}
}
)
} catch (error) {
console.log('文件打开失败', error)
reject({ Status: 'Error', Msg: '文件打开失败!', Error: error })
}
}
这是插件打开文件用的源码
private void _open(String fileArg, String contentType, Boolean openWithDefault, CallbackContext callbackContext)
throws JSONException {
String fileName = "";
try {
CordovaResourceApi resourceApi = webView.getResourceApi();
Uri fileUri = resourceApi.remapUri(Uri.parse(fileArg));
fileName = fileUri.getPath();
} catch (Exception e) {
fileName = fileArg;
}
File file = new File(fileName);
if (file.exists()) {
try {
callbackContext.success();
Intent intent;
if (contentType.equals("application/vnd.android.package-archive")) {
// https://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider/9672282#9672282
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
Uri path;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
path = Uri.fromFile(file);
} else {
Context context = cordova.getActivity().getApplicationContext();
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider",
file);
}
intent.setDataAndType(path, contentType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent = new Intent(Intent.ACTION_VIEW);
Context context = cordova.getActivity().getApplicationContext();
Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider",
file);
intent.setDataAndType(path, contentType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY);
}
/*
* @see http://stackoverflow.com/questions/14321376/open-an-activity-from-a-
* cordovaplugin
*/
if (openWithDefault) {
cordova.getActivity().startActivity(intent);
} else {
cordova.getActivity().startActivity(Intent.createChooser(intent, "打开文件"));
}
} catch (android.content.ActivityNotFoundException e) {
JSONObject errorObj = new JSONObject();
errorObj.put("status", PluginResult.Status.ERROR.ordinal());
errorObj.put("message", "Activity not found: " + e.getMessage());
callbackContext.error(errorObj);
}
} else {
JSONObject errorObj = new JSONObject();
errorObj.put("status", PluginResult.Status.ERROR.ordinal());
errorObj.put("message", "File not found");
callbackContext.error(errorObj);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论