Reputation: 37
I am downloading a file from DropBox.
I can see that the file exists by using File.exists(filePath)
and it returns true
.
But I am not able to open the file (with the default application) with either File.fromPath(filePath)
or with openUrl(filePath)
.
Here is my code:
HomePage.prototype.getDownload = function() {
var filePath = fs.path.join(fs.knownFolders.currentApp().path, "MyFile");
httpModule.getFile({
url: "https://content.dropboxapi.com/2/files/download",
method: "POST",
headers: { "Content-Type": "", "Dropbox-API-Arg": JSON.stringify({"path": "/MyFolder/MyFile"}), "Authorization": "Bearer *********" },
}, filePath).then(function (response) {
console.log("file exists: "+fs.File.exists(filePath)); // This return true
// tried this
fs.File.fromPath(filePath); // Does nothing
// tried this
utilsutils.openUrl(filePath); // Does nothing
}
Upvotes: 1
Views: 4862
Reputation: 831
Since Android API 24+, Android will throw this error: android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
To overcome this error you need to make some changes to Android manifest file and add another file to App_Resources. Here are the steps:
1) Create a folder named xml
under App_Resoures/Android
. Create a file named provider_paths.xml
in it. Paste this into that file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
2) Use this code to open file:
try {
let intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
let mimeType = this.findExtension(file.extension); // The file here is the nativescript file object (fs.File)
let context = application.android.currentContext;
let nativeFile = new java.io.File(file.path);
let uri = android.support.v4.content.FileProvider.getUriForFile(context, 'com.your.appname.provider', nativeFile); // Here add ".provider" after your app package name
intent.setDataAndType(uri, mimeType);
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
application.android.currentContext.startActivity(android.content.Intent.createChooser(intent, 'Open File...'));
} catch (e) {
console.log(e);
}
3) First two steps should be enough. Test if it is working. If not working put this into application
tag in Android Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.your.appname.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Notice that android:authorities should be same as the one you provided in step 2.
Upvotes: 4
Reputation: 1776
Here is another possible solution for iOS by using utils.ios.openFile
:
import * as utils from 'utils/utils';
import * as fs from 'file-system';
...
let fileName = '/test.pdf';
let documents = fs.knownFolders.currentApp();
let file = this.documents.getFile(this.fileName);
let open = utils.ios.openFile(this.documents.path + this.fileName);
After the file is open, you have a Share button with the default applications.
Upvotes: 1
Reputation: 1119
AFAIK openUrl
works only for web links and cannot be used to open local files. And fs.File.fromPath(filePath);
just creates instance of File and does not do anything with it
In order to open the file you need to use the native APIs available for each platform. For example for android (assuming your file is PDF):
try
{
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(filePath)), "application/pdf");
application.android.currentContext.startActivity(android.content.Intent.createChooser(intent, "Open PDF..."));
}
catch (e)
{
console.log("Missing PDF application");
}
Upvotes: 5