【问题标题】:Titanium/Android access a file created in Titanium from native moduleTitanium/Android 从本机模块访问在 Titanium 中创建的文件
【发布时间】:2017-05-02 19:15:07
【问题描述】:

如何从本机模块中访问 Titanium 保存的文件?在我的代码中,我将使用相机 (Ti.Media) 拍摄的照片保存到文件中。然后,我试图从我的模块中读取相同的文件。我将nativePath 传递给模块的方法。但是,我的模块中不断出现未找到文件的错误。

在相机成功回调中,我有这个代码:

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

我尝试了externalStorageDirectoryapplicationDataDirectorytempDirectoryapplicationCacheDirectory,结果都一样。

【问题讨论】:

    标签: titanium titanium-modules titanium-android


    【解决方案1】:

    请删除file:/ 后缀,它是Titanium 专用的东西。 Android中的路径以/开头

    【讨论】:

    • 做到了!我做nativePath.replace('file:', '').replace(/\/\//g, '/') 并将其传递给我的模块,它可以很好地访问该文件。谢谢!
    【解决方案2】:

    这就是我将图像路径转换为文件的方式:

    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 尤其是标记功能。这将转换路径(条形元素)

    【讨论】:

    • 我正在尝试使用 ExifInterface。构造函数接受一个字符串,文档说它指的是文件名。我假设这意味着文件路径,但也许它只是相对于应用程序数据目录的名称。 developer.android.com/reference/android/media/…
    • 啊,好吧,我添加了一个指向 fh.imagefactory 的链接,它可以打开一个文件并访问其 exif 信息(它会删除文件部分)这也应该适合你
    • 谢谢,指向freshheads repo 的链接很棒。这比在应用端做更好,这样模块更容易被 Ti 开发者使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多