![](/img/trans.png)
[英]Save file to Specific folder on SD card (External Storage Removable)
[英]Nativescript: How to save a file in external storage (SD card)
经过大量的谷歌搜索和“上下文外”代码的大量尝试后,我恳求您帮助我。
我试图弄清楚是否可以使用 Nativescript 在外部存储上写入。 我没有兴趣在应用程序上下文中编写。 所以文档显示的内容不是我要找的。
我设法从 Nativescript 论坛上的一个线程中实现了这一点:
android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
它有效,它为我提供了一条路径,但是当我拥有这条路径时,我不知道如何处理它。 如何在该路径内创建文件,读取它等。
我需要实现的是创建一个用户和应用程序都可以轻松访问的文件夹。 用户应该能够使用内置文件资源管理器访问此文件夹。
该应用程序在 Angular 上运行。
您知道您的外部(SD 卡)路径吗? 如果它类似于 /storage/emulated/0,那么您可以尝试创建一个文件夹或文件。
import * as fs from "tns-core-modules/file-system";
let externalPath= fs.path.join(android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString());
//Create a folder with known path
var folder: fs.Folder = fs.Folder.fromPath(sdCardPath+"/test");
//Create a file
var testFile: fs.File = folder.getFile("test.txt");
console.log("Path " + folder.path)
用户应该能够访问此折叠和文件。 它位于设备内部存储中,即“外部”文件夹。
我仍然试图弄清楚如何访问 SD 卡,但希望上面的代码对你有用。
我真的在 Android 设备上遇到了这个问题,最终是由于:
信息
nativescript 6.0.3
tns-core-modules 6.0.7
tns-android 6.0.2
tns 插件
nativescript-permissions 1.3.7
示例代码
import * as fs from "tns-core-modules/file-system"
...
// First get the required permissions
// Note: this permissions should also be in your AndroidManifest.xml file as:
// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
const permissions = require('nativescript-permissions')
permissions.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
.then(() => {
console.log('Required Android permissions have been granted');
})
.catch(() => {
console.error('Required Android permissions have been denied!');
});
// Get the publicly accessable Downloads directory path
const sdDownloadPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString()
console.log('sdDownloadPath: ' + sdDownloadPath)
// Get a specific folder in that path (will be created if it does not exist)
const myAppFolder = fs.Folder.fromPath(fs.path.join(sdDownloadPath, 'myApp'))
console.log('myApp path: ' + myAppFolder.path)
// Get a file in that path (will be created if it does not exist)
// Note: In this case we try to get a unique file every time this code is run
let date = new Date()
date = date.toISOString().replace('.', '')
const myFile = myAppFolder.getFile(`myfile_${date}.txt`)
console.log('myFile path: ' + myFile.path)
// Write some data to this new file
myFile.writeText('Hello duder 123')
.then(() => {})
.catch((err) => console.log(`Error writing to file: ${err}`))
// Try and read back the data that was written
myFile.readText()
.then((res) => {
console.log(`Text read back: ${res}`)
}).catch((err) => {
console.log(err.stack);
});
// List all files in the myApp folder
myAppFolder.getEntities()
.then((entities) => {
// entities is array with the document's files and folders.
entities.forEach((entity) => {
console.log(entity)
});
}).catch((err) => {
console.log(err.stack);
});
安卓文件传输问题
我浪费了很多时间的一个问题是,我可以使用getEntities()
看到文件,但在 Mac 上使用第 3 方工具“Android 文件传输 (AFT)”时却看不到它们。 我最终偶然发现了“Android Studio 的 -> 设备文件资源管理器”,可以看到我创建的所有文件和文件夹,因此意识到问题出在 AFT 上。
我现在使用Airdroid浏览和下载设备文件。
适用参考
https://docs.nativescript.org/angular/ng-framework-modules/file-system (角度文档,但也与 nativescript-vue 相关)
我有同样的问题,最后通过在 AndroidManifest.xml 文件中添加android:requestLegacyExternalStorage="true"
来解决它,请按照此处的线程进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.