【问题标题】:Nativescript and resources folder - is there a simple solution?Nativescript 和资源文件夹 - 有没有简单的解决方案?
【发布时间】:2017-11-28 13:20:14
【问题描述】:

我将从目前的事实开始 - 我正在成功上传 一个 mp3 文件到服务器 - 但是通过一种 hacky 方式。

如您所见,我有 2 个raw 文件夹:

这是上传文件的工作代码:

 upload1()
    {

        let file = fs.path.join(fs.knownFolders.currentApp().path, "raw/a1.mp3");

        var request = {
            url:         "http://posttestserver.com/post.php",
            method:      "POST",
            ...
        };

        var task = session.uploadFile(file, request);
        task.on("progress", this.logEvent);

    }

Here is the output for the successful upload (showing progress)

那么问题出在哪里?

当前文件正在从根raw 文件夹上传。 (proof)

但正确的方法(as I was told)是通过raw文件夹或assets文件夹上传,然后通过res://raw/a1.mp3(或res://raw/a1)引用。

所以我已经切换到:let file="res://raw/a1.mp3";

但是得到了error

错误错误:java.io.FileNotFoundException:找不到文件 路径:res://raw/a1.mp3

也适用于:let file="res://raw/a1"; //without extension - 出现错误:

JS: ERROR 错误: java.io.FileNotFoundException: 找不到文件 路径:res://raw/a1

问题:
如何访问raw 文件夹下的mp3 文件? (不是外部文件夹,而是资源文件夹)

【问题讨论】:

  • 为什么你需要在你的apk中上传一个文件包?您已经可以访问该文件,为什么不直接将它放在您的文件服务器上?
  • @WadeWilson 我还在学习材料。最后 - 我想上传一个在原始文件夹中找到的文件。暂时请把它当作一个生成mp3并保存在RAW文件夹中的APP,需要上传到服务器。
  • 我添加了一个答案,至少可以帮助您指出正确的方向。原始文件夹和 res 目录下的所有文件夹都是只读的,因此您将无法写入它们,因为考虑到 android 构建系统如何处理资源。您需要将生成的 mp3 直接存储到设备存储中。

标签: android angular nativescript


【解决方案1】:

我不熟悉 NativeScript,但这里有一个示例,说明如何使用 Java 访问原始文件夹文件,而不管其类型如何:

public static final FileDescriptor getFDForResource(Context context, int resId) {
     AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
     if (afd != null) {
        return afd.getFileDescriptor();
     }
     return null;
}

public void readFile(int resId) {
    FileDescriptor fd = getFDForResource(resId);
    InputStream inputStream
    if(fd != null) {
        inputStream = new FileInputStream(fd);
        byte nextByte;
        while((nextByte = inputStream.read()) != -1) {
            // Upload the file bytes to your server.
        }
    }
}

您需要将此代码放在 Util 类中并使用 NativeScript 访问它。我确信有一些 API 可以做这样的事情,但我不熟悉,这就是我所能提供的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2023-01-10
  • 2022-01-14
  • 1970-01-01
相关资源
最近更新 更多