Nativescript和资源文件夹-有一个简单的解决方案吗? [英] Nativescript and resources folder - is there a simple solution?
问题描述
我将从当前的事实开始-我上传成功 mp3文件发送到服务器-但是通过黑客方式.
I will start with the fact that currently - I'm uploading successfully an mp3 file to a server - but via a hacky way.
如您所见,我有2个raw
文件夹:
I'm having 2 raw
folders as you can see :
这是上传文件的工作代码:
And here is the working code to upload the file :
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);
}
那么问题出在哪里?
当前文件是从根raw
文件夹上载的. (证明)
The current file is being uploaded from the root raw
folder. (proof)
但是正确的方法( as有人告诉我 )是通过raw
文件夹或assets
文件夹上传它,然后通过res://raw/a1.mp3
(或res://raw/a1
)引用它.
But the right way (as I was told)is to upload it via the raw
folder or the assets
folder and then reference it via res://raw/a1.mp3
( or res://raw/a1
).
所以我切换到了let file="res://raw/a1.mp3";
但是出现了 错误 :
But got an error:
错误错误:java.io.FileNotFoundException:在以下位置找不到文件 路径:res://raw/a1.mp3
ERROR Error: java.io.FileNotFoundException: Could not find file at path: res://raw/a1.mp3
也用于:let file="res://raw/a1"; //without extension
-出现错误:
JS:错误错误:java.io.FileNotFoundException:在以下位置找不到文件 路径:res://raw/a1
JS: ERROR Error: java.io.FileNotFoundException: Could not find file at path: res://raw/a1
问题:
如何访问raw
文件夹下的mp3文件? (不是外部文件夹,而是资源文件夹)
Question:
How can I access the mp3 file under the raw
folder? (not the outside folder but the resource one)
推荐答案
我不熟悉NativeScript,但是这里有一个示例,说明了如何使用Java访问原始文件夹文件,而无论其类型如何:
I am unfamiliar with NativeScript but here is an example of how to access the raw folder files regardless of type using 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可以做到这一点,但是我不熟悉我能提供的一切.
You'll wanna place this code in a Util class and access it using NativeScript. I'm sure there is some API to do such a thing but being I am unfamiliar that is all I can provide.
这篇关于Nativescript和资源文件夹-有一个简单的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!