问题
I am building an app for an LG Smart TV. I need to load json data from a USB drive when it's inserted into the smart TV.
I'm not sure how to do this, never worked on an app like this. My question is how do I gain access to external storage and load json data into my app?
回答1:
Using the SCAP API (v1.2+)
function readFile() {
var successCb = function (cbObject) {
var data_text = cbObject.data;
// do something with the json string
};
var failureCb = function (cbObject) {
var errorCode = cbObject.errorCode;
var errorText = cbObject.errorText;
// do something with the error
};
// Read the whole file from the beginning, as a text file.
var options = {
path: "file://usb:[INDEX]/[FILE_PATH]",
position: 0,
encoding: 'utf8'
};
var storage = new Storage();
storage.readFile(successCb, failureCb, options);
}
The filepath for a usb is file://usb:[INDEX]/[FILE_PATH]
[INDEX] is the usb index (could have multiple USBs connected).
Additional help can be found here: http://webossignage.developer.lge.com/api/scap-api/scap16/storage/?wos_flag=readFile#readFile
You can use getStorageInfo()
to check if your usb is available.
来源:https://stackoverflow.com/questions/58551101/load-json-from-external-storage