2016-12-16 10 views
0

ネイティブモジュール内でTitaniumによって保存されたファイルにアクセスするにはどうすればよいですか?私のコードでは、カメラ(Ti.Media)で撮影した画像をファイルに保存します。次に、私は自分のモジュールから同じファイルを読み込もうとしています。私はnativePathをモジュールのメソッドに渡しています。しかし、私は自分のモジュールでファイルが見つかりませんエラーを取得し続けます。カメラの成功コールバックでチタン/ Androidは、ネイティブモジュールからチタンで作成されたファイルにアクセスします。

、私はこのコードを持っている:

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

私はリソースフォルダからファイルを開くためにそれを使用しています。まだ外部ファイルで試してみませんでしたか?

ファイルをロードする場所にモジュールコードが表示されますか? https://github.com/freshheads/fh.imagefactory/blob/master/android/src/fh/imagefactory/ImagefactoryModule.java#L66 、特に顕著な機能:

編集は、このプロジェクトを見ているExif情報のためのファイルを使用します。それはパス(ストリップ要素)を変換します

+0

私はExifInterfaceを使用しようとしています。コンストラクタはドキュメントがファイル名を参照するStringを受け取ります。私はそれがファイルパスを意味すると仮定していたかもしれませんが、多分それは単にアプリケーションのデータディレクトリに関連した名前です。 https://developer.android.com/reference/android/media/ExifInterface.html#ExifInterface(java.lang.String) – skypanther

+0

ああ、ファイルを開き、そのexifにアクセスするfh.imagefactoryへのリンクを追加しました情報(それはファイルの部分を削除します)それはあなたのためにも働くはずです – miga

+0

ありがとう、freshheadsレポへのリンクは素晴らしいです。これは、Tiデベロッパーがモジュールを使用しやすいように、アプリ側よりも優れています。 – skypanther

2

接尾辞file:/を削除してください、それはチタン固有のものです。 Androidのパスは/で始まり、

+0

それはそれをしました!私は 'nativePath.replace( 'file:'、 '').replace(/ \/\ // g、 '/')'を実行し、それを私のモジュールに渡し、ありがとう! – skypanther

関連する問題