【问题标题】:How to download and open a file with NS 6.0如何使用 NS 6.0 下载和打开文件
【发布时间】:2019-07-19 16:15:58
【问题描述】:

我已经迁移到 NativeScript 6.0,需要一些帮助,了解如何在 Downloads 文件夹中下载和打开带有 Android 支持库 (AndroidX) 的文件。 p>

实际上,在 NS 5.x 中,我使用了 Android 支持库 (android.support.v4.content.FileProvider) 中的 FileProvider 并且效果很好。迁移后,使用 (androidx.core.content.FileProvider),我打开应用程序时出错。

但在 Android 文档中,我找不到任何方法或信息来迁移本机下载和打开(下载文件夹)的代码。


上一个方法:

  private openFile(fileName: string, mimeType: string, extension: string) {
    try {
      if (isAndroid) {
        const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
        const context = applicationModule.android.context;
        console.log("android.ctx=", context);

        const nativeFile = new java.io.File(fileName);
        console.log("nativeFile=", nativeFile);

        const uri = android.support.v4.content.FileProvider.getUriForFile(context, "com.otisw.gescon.app.provider", nativeFile);

        intent.setDataAndType(uri, mimeType);
        intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);

        const choosedIntent = android.content.Intent.createChooser(intent, "Open file...");
        console.log("choosedIntent=>", choosedIntent);
        context.startActivity(choosedIntent);
      } else {
        // const documents = fs.knownFolders.currentApp();
        // const file = this.documents.getFile(fileName);
        const open = utils.ios.openFile(fileName);
      }
    } catch (e) {
      console.log(e);
    }
  }

试过了:

  private openFile(fileName: string, mimeType: string, extension: string) {
    try {
      if (isAndroid) {
                const intent = androidx.core.content.IntentCompat.makeMainSelectorActivity(
                    "android.content.Intent.ACTION_VIEW",
                    "??"
                );

文件参考.d.ts:

/// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" />

/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android/androidx-26.d.ts" />

是否有人尝试将用于下载和打开的代码从 NativeScript 升级到新的 AndroidX 或知道解决方法?

谢谢!

【问题讨论】:

    标签: nativescript androidx


    【解决方案1】:

    您必须在 AndoridX 上使用 androidx.core.content.FileProvider,在较低版本中使用 android.support.v4.content.FileProvider

    随着 Android 9.0(API 级别 28)的发布,有一个新版本 称为 AndroidX 的支持库,它是 Jetpack 的一部分。这 AndroidX 库包含现有的支持库以及 包括最新的 Jetpack 组件。

    您可以继续使用支持库。历史文物 (那些版本为 27 及更早版本,并打包为 android.support.*) 将在 Google Maven 上保持可用。但是,所有新图书馆 开发将在 AndroidX 库中进行。

    我们建议在所有新项目中使用 AndroidX 库。你 还应该考虑将现有项目也迁移到 AndroidX。

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 2018-10-14
      相关资源
      最近更新 更多