[英]How to read text file from sdCard on ionic2 and android 5?
I am trying to open a text file and put the content in sqlite db. 我正在尝试打开一个文本文件并将内容放在sqlite db中。 The issue is that I cant read the file in any way.
问题是我无法以任何方式阅读该文件。 Below is closest to success I managed to accomplish.
以下是我设法完成的最接近的成功。 When executed it produces FileError(2): "SECURITY_ERR".
执行时会产生FileError(2):“SECURITY_ERR”。
openFile(): void {
FileChooser.open()
.then(uri => {
console.log(uri);
File.resolveLocalFilesystemUrl(uri)
.then(entry=>{
console.log(entry);
let path = entry.nativeURL.substring(0, entry.nativeURL.lastIndexOf('/'));
console.log(path);
File.readAsText(path, entry.name)
.then(content=>{
console.log(content);
})
})
.catch(e => console.log(e));
});
}
On phone I see that the app has rw access on sd card and in platform/AndroidManifest.xml
i have set: 在手机上我看到该应用程序在sd卡和我设置的
platform/AndroidManifest.xml
有rw访问权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I've spent lot of time digging through the internet and trying different solutions with no success. 我花了很多时间在互联网上挖掘并尝试不同的解决方案但没有成功。
Edit: 编辑:
Here is what console.log(cordova.file);
这是
console.log(cordova.file);
outputs: 输出:
applicationDirectory: "file:///android_asset/"
applicationStorageDirectory: "file:///data/data/com.ionicframework.leki645195/"
cacheDirectory: "file:///data/data/com.ionicframework.leki645195/cache/"
dataDirectory: "file:///data/data/com.ionicframework.leki645195/files/"
documentsDirectory: null
externalApplicationStorageDirectory: "file:///storage/emulated/0/Android/data/com.ionicframework.leki645195/"
externalCacheDirectory: "file:///storage/emulated/0/Android/data/com.ionicframework.leki645195/cache/"
externalDataDirectory: "file:///storage/emulated/0/Android/data/com.ionicframework.leki645195/files/"
externalRootDirectory: "file:///storage/emulated/0/"
sharedDirectory: null
syncedDataDirectory: null
tempDirectory: null
Try this code, 试试这段代码,
openFile() {
this.fileChooser.open()
.then(uri => {
this.file.resolveLocalFilesystemUrl(uri)
.then(entry=>{
let path = entry.nativeURL;
//read file contents
var rawFile = new XMLHttpRequest();
rawFile.open("GET", path, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var fileText = rawFile.responseText;
alert(fileText);
}
}
}
rawFile.send(null);
})
.catch(e => console.log(e));
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.