3

我正在尝试将照片库中的图像保存到本地存储,以便我可以跨应用程序会话加载图像。一旦用户完成选择图像,就会执行以下逻辑。在模拟器上,我看到错误消息已写入日志。即使我看到错误消息,我认为图像仍然保存在模拟器中,因为当我重新启动应用程序时,我能够加载保存的图像。但是,当我在设备上运行它时,我仍然收到您在下面的代码中看到的错误消息,并且加载了默认背景,这表明写入不成功。

谁能看到我做错了什么以及为什么图像无法成功保存?

var image = i.media.imageAsResized(width, height);
backgroundImage.image = image;      
function SaveBackgroundImage(image)
        {
            var file = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,W.CUSTOM_BACKGROUND);
            if(file.write(image, false))
            {
                W.analytics.remoteLog('Success Saving Background Image');
            }
            else
            {
                W.analytics.remoteLog('Error Saving Background Image');
            }
            file = null;            
        }
4

1 回答 1

4

试试这个代码:

var parent = Titanium.Filesystem.getApplicationDataDirectory();
var f = Titanium.Filesystem.getFile(parent, 'image_name.png');
f.write(image);
Ti.API.info(f.nativePath); // it will return the native path of image

在您的代码中,您没有给出图像的类型(png/jpeg),这就是您收到错误的原因。

于 2012-12-05T14:30:56.520 回答