英文:
How to get app name programatically in native script
问题
我正在尝试将一些文件保存在我的本地文件存储中,所以我正在进行类似以下的操作:
var folder_name = "abcde/" + viewModel.dir_path;
const documents = fileSystemModule.knownFolders.documents();
documents._path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
const folder = documents.getFolder(folder_name);
var file = fileSystemModule.path.join(folder._path, this.pdf_url.split("/").pop());
var url = this.pdf_url;
httpModule.getFile(url, file).then(function(result) {
console.log(result);
Toast.makeText(`${result._name} 已成功下载到 ${folder_name}`).show();
}, function(e) {
console.log(e);
});
唯一的问题是硬编码的值 abcde/
,我希望它是“应用程序名称”。无论应用程序名称是什么,它都应该使用该名称。
我找不到以编程方式读取应用程序名称的方法。我需要这个在Android上,我对IOS不感兴趣。
英文:
I'm trying to save some files on my local File storage So, I'm doing Something like below
var folder_name = "abcde/" + viewModel.dir_path;
const documents = fileSystemModule.knownFolders.documents();
documents._path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
const folder = documents.getFolder(folder_name);
var file = fileSystemModule.path.join(folder._path, this.pdf_url.split("/").pop());
var url = this.pdf_url;
httpModule.getFile(url, file).then(function(result) {
console.log(result);
Toast.makeText(`${result._name} is succesfully downloaded in ${folder_name}`).show();
}, function(e) {
console.log(e);
});
the only problem is the hardcoded value abcde/
I want it to be app name
. whatever the app name is it should take that name.
I don't find any ways to read app name programatically. I need this to Android I'm not interested in IOS.
答案1
得分: 0
这可能是一个答案。但这仍然不是一个合适的方式。
const documents = fileSystemModule.knownFolders.currentApp();
console.log(documents); --> "/data/data/org.nativescript.app_name/files/app"
var str = documents;
var arr = str.split("/")[3];
console.log(arr.split(".")[2]); --> app_name
英文:
may be this is one could be the answer. but still this is not a proper way
const documents = fileSystemModule.knownFolders.currentApp();
console.log(documents); --> "/data/data/org.nativescript.app_name/files/app"
var str = documents;
var arr = str.split("/")[3];
console.log(arr.split(".")[2]); ---> app_name
答案2
得分: 0
在Android中,您可以在strings.xml
中设置应用程序的应用名称,例如:
<string name="app_name">App_Name</string>
然后,每当您想要重用应用名称时,您可以在应用程序中使用getString
方法并提供其名称。
英文:
In Android, you can set the app name for the application in strings.xml
, for example:
<string name="app_name">"App_Name"</string>
Then whenever you want to reuse the app name, you can use the getString
method with its name in the application.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论