简体   繁体   English

Titanium / Android从本地模块访问在Titanium中创建的文件

[英]Titanium/Android access a file created in Titanium from native module

How do I access a file saved by Titanium from within a native module? 如何从本机模块中访问Titanium保存的文件? In my code, I save a picture taken with the camera (Ti.Media) to a file. 在我的代码中,我将用相机(Ti.Media)拍摄的照片保存到文件中。 Then, I'm trying to read that same file from my module. 然后,我试图从我的模块中读取相同的文件。 I'm passing the nativePath to the module's method. 我正在将nativePath传递给模块的方法。 But, I keep getting file not found errors in my module. 但是,我一直在模块中找不到文件错误。

In the camera success callback, I have this code: 在相机成功回调中,我有以下代码:

// 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 ...
    }, ...
}) 

I see this in the logs: 我在日志中看到了这一点:

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

I have tried with externalStorageDirectory , applicationDataDirectory , tempDirectory , and applicationCacheDirectory all with the same results. 我已经尝试使用externalStorageDirectoryapplicationDataDirectorytempDirectoryapplicationCacheDirectory获得相同的结果。

Please remove the file:/ suffix, it is a Titanium specific stuff. 请删除file:/后缀,它是Titanium专用的东西。 Paths in Android begins with / Android中的路径以/开头

This is how I convert a image path to a file: 这是我将图像路径转换为文件的方式:

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

I'm using it to open a file from the resource folder. 我正在用它来打开资源文件夹中的文件。 Didn't try it with an external file yet. 尚未尝试使用外部文件。

Could you perhaps show your module code where you load the file? 您能否在加载文件的地方显示模块代码?

Edit 编辑

To use a file for Exif information have a look at this project: https://github.com/freshheads/fh.imagefactory/blob/master/android/src/fh/imagefactory/ImagefactoryModule.java#L66 and especially the marked function. 要将文件用于Exif信息,请查看以下项目: https : //github.com/freshheads/fh.imagefactory/blob/master/android/src/fh/imagefactory/ImagefactoryModule.java#L66 ,尤其是标记功能。 That will convert the path (strip elements) 这将转换路径(带状元素)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM