cordova-plugin-file-opener2插件安卓第一次使用第三方应用打开文件同意权限后打开失败

发布于 2022-09-11 21:26:39 字数 4345 浏览 24 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文