【问题标题】:Writin base64encoded image to file in titanium将base64编码图像写入钛文件
【发布时间】:2014-08-01 23:46:28
【问题描述】:

问题是如何将标准 base64 类型的图像数据从 webview 写入外部存储上的文件(在我的情况下为 jpeg)?

Ti.App.addEventListener('app:save_op', function(d) { //listens events fired from js in webview

                        var base64img = d.url;
                        var imageBlob = Ti.Utils.base64decode(base64img);


                        var imageView = Titanium.UI.createImageView({
                            image:imageBlob,
                            // top:20,
                            // width:300,
                            // height:100
                        });

                        self.add(imageView);


                        if (Ti.Filesystem.isExternalStoragePresent()) {

                            // var theMap = win1.toImage();
                              // var file = Titanium.Filesystem.createTempFile(Titanium.Filesystem.resourcesDirectory);
                              // Ti.API.info('size = ' + file.size);
                              // file.write(theMap);



                            fname = new Date().getTime() + '_' + 'out.jpg';
                            fname2 = new Date().getTime() + '_' + 'o.jpg';

                            var f = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, fname);
                            var f2 =Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, fname2);
                            // f2 = f.read();
                            // f2.write();

                            f.write(d.url);
                            //
                            //f.write( imageBlob );

                            // var img = new Image();
                            // img.src = imageBlob; 

                            f.write(imageView.image);
                            alert("nativePath = " + f.nativePath);

我实现事件侦听器以从 webview 中的 fireevent 捕获 jpeg 文件的 base64 字符串....但它只将它们作为纯文本写入文件,而不是图像 } 我已经尝试了很多方法来做到这一点:toImage/ blob--->buffer --->file / base64decode.toString....

【问题讨论】:

    标签: file webview titanium base64


    【解决方案1】:

    如果您只是尝试将图像保存到设备,您可以尝试:

    Titanium.Media.saveToPhotoGallery(imageBlob);
    

    这里是documentation

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 2016-01-13
      • 1970-01-01
      • 2013-01-31
      • 2011-04-12
      • 2010-12-04
      相关资源
      最近更新 更多