2016-12-16 32 views
0

如何從本機模塊訪問由Titanium保存的文件?在我的代碼中,我將用相機拍攝的照片(Ti.Media)保存到文件中。然後,我試圖從我的模塊中讀取同一個文件。我將nativePath傳遞給模塊的方法。但是,我不斷收到我的模塊中找不到文件錯誤。Titanium/Android訪問由本機模塊在Titanium中創建的文件

在相機成功回調,我有這樣的代碼:

// earlier in the code 
var tiexif = require('com.me.tiexif'); 

Ti.Media.showCamera({ 
    success: function(event) { 
     console.log("TIEXIF: showCamera.success()"); 
     anImageView.image = event.media; // works 
     if (Ti.Filesystem.hasStoragePermissions()) { 
      console.log("TIEXIF: hasStoragePermissions"); 
      var file = Titanium.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'testphoto.jpg'); 
      console.log("TIEXIF: nativePath: " + file.nativePath); 
      file.write(event.media); 
      someUILabel.text = tiexif.getExifOrientation(file.nativePath); 
     } else ... 
    }, ... 
}) 

我看到這在日誌中:

[INFO] TIEXIF: showCamera.success() 
[ERROR] File: fail readDirectory() errno=20 
[ERROR] File: fail readDirectory() errno=13 
[ERROR] File: fail readDirectory() errno=13 
[ERROR] File: fail readDirectory() errno=13 
[INFO] TIEXIF: hasStoragePermissions 
[INFO] TIEXIF: nativePath: file:///storage/emulated/0/com.me.exiftest/testphoto.jpg 
[WARN] ExifInterface: Invalid image. 
[WARN] ExifInterface: java.io.FileNotFoundException: file:/storage/emulated/0/com.me.exiftest/testphoto.jpg: open failed: ENOENT (No such file or directory) 

我試圖與externalStorageDirectoryapplicationDataDirectorytempDirectory,並applicationCacheDirectory所有相同的結果。

回答

0

這是我如何轉換爲圖像文件路徑:

private String getPathToApplicationAsset(String assetName) { 
    // The url for an application asset can be created by resolving the specified 
    // path with the proxy context. This locates a resource relative to the 
    // application resources folder 

    String result = resolveUrl(null, assetName); 
    return result; 
} 

// ... 
String imageSrc = options.getString("image"); 
// ... 
String url = getPathToApplicationAsset(imageSrc); 
TiBaseFile file = TiFileFactory.createTitaniumFile(new String[] { url }, false); 

我用它來打開從資源文件夾中的文件。沒有用外部文件嘗試它。

你可能會顯示你的模塊代碼加載文件的位置?

編輯

要使用的Exif信息的文件來看看這個項目: https://github.com/freshheads/fh.imagefactory/blob/master/android/src/fh/imagefactory/ImagefactoryModule.java#L66 ,特別是顯着的作用。這將轉換路徑(條元素)

+0

我正在嘗試使用ExifInterface。構造函數接受一個文檔所指的文件名。我假設這意味着文件路徑,但也許它只是一個相對於應用程序數據目錄的名稱。 https://developer.android.com/reference/android/media/ExifInterface.html#ExifInterface(java.lang.String) – skypanther

+0

好的,我添加了一個fh.imagefactory的鏈接,它打開一個文件並訪問它的exif信息(它會刪除文件的一部分)這應該也適用於你 – miga

+0

謝謝,這與freshheads回購的鏈接是偉大的。這比完成應用程序更好,這樣該模塊對於Ti開發人員來說更容易使用。 – skypanther

2

請刪除file:/後綴,它是一個鈦特定的東西。 Android中的路徑始於/

+0

這樣做!我做'nativePath.replace('file:','').replace(/ \/\ // g,'/')'並將它傳遞給我的模塊,它可以很好地訪問文件。謝謝! – skypanther

相關問題