2016-04-07 11 views
0

DropBoxからファイルをダウンロードしています。デフォルトのアプリケーションでファイルを開く方法

File.exists(filePath)を使用してファイルが存在し、それがtrueを返すことがわかります。 しかし、File.fromPath(filePath)またはopenUrl(filePath)のいずれかのファイル(既定のアプリケーションで)を開くことができません。ここで

は私のコードです:Webリンクのためにのみ動作し、ローカルファイルを開くために使用することはできませんopenUrl

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 
} 

答えて

3

私の知る限り。そしてfs.File.fromPath(filePath);は単にFileのインスタンスを作成し、何もしません

ファイルを開くには、各プラットフォームで使用できるネイティブAPIを使用する必要があります。アンドロイドのための例えば(あなたのファイルを仮定すると、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"); 
} 
0

ここutils.ios.openFileを使用して、iOS用の別の可能な解決策は次のとおりです。

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); 

ファイルを開いた後、あなたと共有]ボタンを持っていますデフォルトアプリケーション。

0

AndroidのAPI 24+ので、Androidは、このエラーがスローされます。android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

マニフェストファイルをAndroidとApp_Resourcesに別のファイルを追加するためにいくつかの変更を加える必要があり、このエラーを克服するために。手順は次のとおりです。

1)App_Resoures/Androidの下にxmlという名前のフォルダを作成します。そこにprovider_paths.xmlという名前のファイルを作成します。そのファイルにこれを貼り付けます。

<?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)は、ファイルを開くには、このコードを使用します。

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)最初の2つのステップは十分なはずです。それが動作しているかどうかテストします。当局手順2

で提供したものと同じでなければなりません:Androidがいること

<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> 

お知らせ:Androidのマニフェストでapplicationタグにこれを入れて作業している場合ではありません

関連する問題