【问题标题】:Save image file in sdcard in firefox OS在 Firefox OS 中将图像文件保存在 sdcard 中
【发布时间】:2014-03-27 07:01:12
【问题描述】:

我正在尝试将图像保存在 sdcard 中。我正在关注this 文档。

$('.btnSave').on('click', function () {

        var imageRawData = canvas.toDataURL("image/png") ;
        var sdcard = navigator.getDeviceStorage("sdcard");
        var file = new Blob([imageRawData], { type: "image/png" });
        var request = sdcard.addNamed(file, "FilertedImage.png");

                request.onsuccess = function () {
                var name = this.result;
                console.log('File "' + name + '" successfully wrote on the sdcard storage area');

                }
                request.onerror = function (e) {
                console.log('Unable to write the file: ' + e.target.error.name);

                }
    }); 

在文档中我发现 "pictures 只接受带有有效image mime type"的 Blob。那么如何使用 javascript 将 imageRawData 转换为有效的图像 mime 类型。

【问题讨论】:

    标签: javascript firefox-os


    【解决方案1】:

    我已经这样做了 - 保存然后分享:

     function saveAndSend(blob) {
         var sdcard = navigator.getDeviceStorage("sdcard");
         var request = sdcard.addNamed(blob, "test/mycanvas.png");
    
         //could just share the blob instead of saving
         request.onsuccess = function () {
             var sharingImage = new MozActivity({
                 name: "share",
                 data: {
                     type: "image/*",
                     number: 1,
                     blobs: [blob],
                     filenames: ["mycanvas.png"],
                     filepaths: ["test/mycanvas.png"]
                 }
             });
         }
    
         // An error could occur if a file with the same name already exist
         request.onerror = function () {
             alert('Unable to write the file: ' + this.error.name);
         }
    
     }
    
    
     var cnv = document.getElementById('myCanvas');
     cnv.toBlob(function (blob) {
    
         //var sdcard = navigator.getDeviceStorage("pictures");
         var sdcard = navigator.getDeviceStorage("sdcard");
         var request = sdcard.delete("test/mycanvas.png");
         //try to delete in case it exists
         request.onsuccess = function () {
             saveAndSend(blob);
         }
    
         request.onerror = function () {
             saveAndSend(blob);
         }
    
    
     });
    

    【讨论】:

    • 感谢您的回复。我访问了你的个人资料,看到你已经回答了很多关于 Firefox os 的问题。好人!!!!!。所以另一个请求......请查看stackoverflow.com/questions/22682254/…。我需要这个。
    • 我看了那个问题。亚当的方法有什么错误。
    • 实际上我想知道应用程序域的值,打包应用程序的平台。
    • 在那个问题上,他发布了一个指向 github 示例的链接:github.com/lukasz-madon/heroesgenerator 使用该示例和打包的应用程序,您很可能需要在清单中使用 orign 标记。看看developer.mozilla.org/en-US/Apps/Build/Manifest#origin
    • origin标签对我来说有什么价值?我试过 app://atishkumarphotofilter.com 但 facebook 不接受。
    【解决方案2】:

    您的应用还需要确保它具有适当的设备存储权限。

    例如,请参阅:https://github.com/mozilla-b2g/gaia/blob/master/dev_apps/ds-test/manifest.webapp#L13。 ds-test 是我编写的一个测试应用程序,用于测试设备存储中的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      相关资源
      最近更新 更多