0

如何从本机模块中访问 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)

我试过用externalStorageDirectory, applicationDataDirectory, tempDirectory, 和applicationCacheDirectory所有的结果都是一样的。

4

2 回答 2

2

请删除file:/后缀,它是 Titanium 特定的东西。Android 中的路径以/

于 2016-12-20T11:11:26.797 回答
1

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

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 ,尤其是标记的函数. 这将转换路径(条形元素)

于 2016-12-19T17:28:08.453 回答