【问题标题】:Phonegap: save base64 image to galleryPhonegap:将base64图像保存到图库
【发布时间】:2015-03-26 20:54:59
【问题描述】:

我只想保存一张图片 base64 并在图库中打开它。我不想从图库中获取图片或拍照。

<img src="base64" />
<button>Save</button>

phonegap + angularjs

谢谢!

【问题讨论】:

    标签: cordova phonegap-plugins


    【解决方案1】:

    解决方案:

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    
      var fileTransfer = new FileTransfer();
      var uri = encodeURI("http://www.example.com/image");
      var path = fileSystem.root.toURL() + "appName/example.jpg";
    
      fileTransfer.download(
        uri,
        path,
        function(entry) {
          refreshMedia.refresh(path); // Refresh the image gallery
        },
        function(error) {
          console.log(error.source);
          console.log(error.target);
          console.log(error.code);
        },
        false,
        {
          headers: {
            "Authorization": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
          }
        }
      );
    
    });
    

    我需要创建一个插件来刷新图片库,因为当您在 android 设备上保存图片时,此图片不会出现在图库中。这个插件会更新图片库。

    refreshMedia.refresh(path); // Refresh the image gallery
    

    插件:https://github.com/guinatal/refreshgallery

    【讨论】:

    • 如何获得“授权”?,可选还是必须?,谢谢
    【解决方案2】:

    CLI:cordova 插件添加 org.apache.cordova.camera

      document.addEventListener("deviceready",onDeviceReady,false);
    
        var pictureSource;   // picture source
        var destinationType; // sets the format of returned value
        function onDeviceReady() {
            pictureSource=navigator.camera.PictureSourceType;
            destinationType=navigator.camera.DestinationType;
        }
    
        $(document).on('click','#pic_parse',function(){
          navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
            encodingType: Camera.EncodingType.PNG,
            destinationType: destinationType.DATA_URL,
            sourceType: pictureSource.SAVEDPHOTOALBUM });
        });
    
        function onPhotoDataSuccess(imageData) { 
            alert(imageData);//here, imageDate is base64.Now, you can save base64.
        }
    
    function onFail(message) {
      alert('Failed because: ' + message);
    }
    

    希望对你有帮助!

    【讨论】:

    • 我不想拍照。我已经有了我的 base64 图像。我只是想把它保存在画廊里。
    • @guinatal 你得到了吗?
    • @DaniloOliveira 实际上我已经遵循了这个:stackoverflow.com/questions/11954604/… 它工作正常,但下载后画廊没有重新加载。我需要重启安卓设备才能让图片出现在图库中。
    【解决方案3】:

    要在 cordova 上下载 base64 图像,请使用此插件,该插件也将您的图像扫描到图库中。

    https://github.com/agomezmoron/cordova-save-image-gallery

    function onDeviceReady() {
        var params = {data: base64String, prefix: 'myPrefix_', format: 'JPG', quality: 80, mediaScanner: true};
        window.imageSaver.saveBase64Image(params,
            function (filePath) {
              console.log('File saved on ' + filePath);
            },
            function (msg) {
              console.error(msg);
            }
          );
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 1970-01-01
      • 2016-11-27
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多