share.uvue 1.8 KB
Newer Older
W
wanganxp 已提交
1
<template>
2 3 4 5
  <view id="viewshot">
    <button class="button" @click="shareText('https://uniapp.dcloud.io/uni-app-x','分享到')">分享文本</button>
    <button class="button" @click="shareSnapShot">指定view截图并分享</button>
  </view>
W
wanganxp 已提交
6 7 8
</template>

<script>
9
  import Intent from 'android.content.Intent';
H
hdx 已提交
10
  import File from 'java.io.File';
W
wanganxp 已提交
11

12 13 14
  export default {
    data() {
      return {
W
wanganxp 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      }
    },
    methods: {
      shareText(text : string, title : string) {
        const context = UTSAndroid.getUniActivity()!;
        const intent = new Intent(Intent.ACTION_SEND)
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        context.startActivity(Intent.createChooser(intent, title));
        // 这里是简单的文本分享示例,如需分享文件图片,需要使用fileProvider
      },
      shareSnapShot() {
        uni.getElementById("viewshot")?.takeSnapshot(
          {
            success: function (res) {
              // 打印截图文件临时路径
              console.log(res.tempFilePath)
              const context = UTSAndroid.getUniActivity()!;
              const intent = new Intent(Intent.ACTION_SEND)
              intent.setType("image/*");
              let file = new File(res.tempFilePath)
W
wanganxp 已提交
37
              const uri = UTSAndroid.getFileProviderUri(file) //3.99支持
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
              intent.putExtra(Intent.EXTRA_STREAM, uri);
              context.startActivity(Intent.createChooser(intent, "分享到"));
            },
            fail: function (res) {
              console.log(res)
              uni.showToast({
                icon: 'error',
                title: '截图失败'
              })
            }
          }
        )
      },

    }
  }
W
wanganxp 已提交
54 55 56
</script>

<style>
57
  .button {
H
hdx 已提交
58
    margin: 15px;
59
  }
W
wanganxp 已提交
60
</style>