找回密码
 立即注册
搜索

如何实现长按图片保存到相册

1
回复
762
查看
[复制链接]

63

主题

68

帖子

655

积分

 楼主| 2022-1-5 10:09:37 来自手机 显示全部楼层 |阅读模式
有没有长按图片可以保存的接口
回复

使用道具 举报

11

主题

124

帖子

675

积分

2022-1-5 14:57:13 显示全部楼层

长按图片保存到相册是一个常用的功能,目前华为快应用还没有现成的接口可以实现,但可以通过通用事件longpress来实现。

实现方法

使用image组件渲染图片,然后在image组件上实现longpress事件,从而触发将图片保存到手机相机的功能。

详细示例代码如下:

<template>
  <div class="doc-page">
    <div class="page-title-wrap">
      <text class="page-title">{{componentName}}</text>
    </div>
    <div class="item-container">      
      <div class="item-content">
        <image src="https://tse1-mm.cn.bing.net/th/id/OIP.QFyNrABM6FhaY_0TCuUZqgHaFj?pid=Api&rs=1" id="image" style="object-fit:cover" onlongpress="onImageLongpress"></image>
      </div>
    </div>
  </div>
</template>
<style>
  .doc-page {
    flex: 1;
    flex-direction: column;
  }
  .item-container {
    margin-top: 40px;
    margin-bottom: 40px;
    flex-direction: column;
  }
  .item-title {
    padding-left: 30px;
    padding-bottom: 20px;
    color: #aaaaaa;
  }
  .item-content {
    height: 200px;
    justify-content: center; 
  }
  #image {
    width: 240px;
    height: 160px;
    object-fit: contain;
  }
</style>
<script>
import prompt from'@system.prompt'
import media from '@system.media'
export default{
  private: {
        componentName:"Touch and hold the following picture to save to the album:",
    inputImageURL: 'https: //tse1-mm.cn.bing.net/th/id/OIP.QFyNrABM6FhaY_0TCuUZqgHaFj?pid=Api&rs=1'
  },
  onInit(){
        this.$page.setTitleBar({text: 'Demo'});
  },
  onImageLongpress(){
        var that=this;
    prompt.showDialog({
      message: 'Touch and hold to save the picture?',
      buttons: [{
        text: 'OK',
        color: '#33dd44'
      },
      {
        text: 'Cancel',
        color: '#33dd44'
      }],
      success: function(data){
                console.log("handling callback",data);
                if(data.index===0)
                {
                    that.$element("image").toTempFilePath({
                        fileType: 'jpg', 
                        quality: 1.0, 
                        success: function (ret) {
                            console.log('toTempFilePath success:tempFilePath=' + ret.tempFilePath)
                            media.saveToPhotosAlbum ({
                                uri: ret.tempFilePath,
                                success:function(data)
                                {
                                    console.log("save picture success");
                                },
                                fail: function(data, code) {
                                    console.log("handling fail, code=" + code);
                                }
                            })
                        }, 
                        fail: function (msg, code) {
                            console.log('toTempFilePath failed:code=' + code + '; msg=' + msg);
                        }, complete: function (ret) {
                            console.log('toTempFilePath complete');
                        }
                    })
                }
      },
      cancel: function(){
        console.log("cancel");
      }
    })
  }
}
</script>


更多案例可参考https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-case-0000001082020374#section1198213111817

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册