提交 7c722ee4 编写于 作者: M mahaifeng

[share]添加系统分享示例

上级 577340d1
<template> <template>
<!-- #ifdef APP -->
<scroll-view direction="vertical" style="flex:1; margin-bottom: 50px;">
<!-- #endif -->
<view id="viewshot"> <view id="viewshot">
<button class="button" @click="shareText('https://uniapp.dcloud.io/uni-app-x','分享到')">分享文本</button> <button class="button" @click="shareText()">分享文本</button>
<button class="button" @click="shareSnapShot">指定view截图并分享</button> <button class="button" @click="shareLink()">分享链接</button>
<button class="button" @click="sharePrivateImg()">分享单个本地图片</button>
<button class="button" @click="sharePrivateImgs()">分享多个本地图片</button>
<button class="button" @click="shareAll()">分享链接、文本、一张图片</button>
<button class="button" @click="sharePrivateVideo()">分享video文件(单个)</button>
<button class="button" @click="sharePrivateVideos()">分享video文件(多个)</button>
<button class="button" @click="sharePrivateAudio()">分享Audio文件(单个)</button>
<button class="button" @click="sharePrivateAudios()">分享Audio文件(多个)</button>
<button class="button" @click="sharePrivateFile()">分享文件(单个)</button>
<button class="button" @click="sharePrivateFiles()">分享文件(多个)</button>
<button class="button" type="primary" @click="sharePrivateErrorImg()">分享单个本地图片(错误路径)</button>
<button class="button" type="primary" @click="sharePrivateErrorImgs()">分享多个本地图片(含有错误路径)</button>
<button class="button" type="primary" @click="sharePrivateErrorVideos()">分享Video文件(错误路径)</button>
<button class="button" type="primary" @click="sharePrivateErrorAudios()">分享Audio文件(错误路径)</button>
<button class="button" type="primary" @click="sharePrivateErrorFiles()">分享文件(错误路径)</button>
<button class="button" type="warn" @click="sharePubImg()">选择图片并分享</button>
<button class="button" type="warn" @click="sharePubMedias()">选择video并分享</button>
<button class="button" type="warn" @click="shareSnapShot()">指定view截图并分享</button>
</view> </view>
</scroll-view>
</template> </template>
<script> <script>
import Intent from 'android.content.Intent';
import File from 'java.io.File';
export default { export default {
data() { data() {
return { return {
summary: '欢迎使用hello uniapp-x'
} }
}, },
methods: { methods: {
shareText(text : string, title : string) { sharePrivateErrorAudios() {
const context = UTSAndroid.getUniActivity()!; uni.hideToast()
const intent = new Intent(Intent.ACTION_SEND) const path1 : string = "/static/test-audio/ForElise.mp3";
intent.setType("text/plain"); const path2 : string = "/static/test-audio/ForElise.mp32";
intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(intent, title)); uni.shareWithSystem({
// 这里是简单的文本分享示例,如需分享文件图片,需要使用fileProvider audioPaths: [path1, path2],
type:'audio',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateErrorFiles() {
uni.hideToast()
const path1 : string = "/static/filemanager1/to.zip";
const path2 : string = "/static/filemanager/11.txt.br";
uni.shareWithSystem({
filePaths: [path1, path2],
type:'file',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateFile() {
uni.hideToast()
const path1 : string = "/static/filemanager/to.zip";
uni.shareWithSystem({
filePaths: [path1],
type:'file',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateFiles() {
uni.hideToast()
const path1 : string = "/static/filemanager/to.zip";
const path2 : string = "/static/filemanager/1.txt.br";
uni.shareWithSystem({
filePaths: [path1, path2],
type:'file',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateAudio() {
uni.hideToast()
const path1 : string = "/static/test-audio/ForElise.mp3";
uni.shareWithSystem({
audioPaths: [path1],
type:'audio',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateAudios() {
uni.hideToast()
const path1 : string = "/static/test-audio/ForElise.mp3";
const path2 : string = "/static/test-audio/ForElise.mp3";
uni.shareWithSystem({
audioPaths: [path1, path2],
type:'audio',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateErrorVideos() {
uni.hideToast()
const path1 : string = "/static/test-video/10second-demo.mp4";
const path2 : string = "/static/test-video/10second-demo1.mp4";
uni.shareWithSystem({
videoPaths: [path1, path2],
type:'video',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateVideo() {
uni.hideToast()
const path1 : string = "/static/test-video/10second-demo.mp4";
uni.shareWithSystem({
videoPaths: [path1],
type:'video',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateVideos() {
uni.hideToast()
const path1 : string = "/static/test-video/10second-demo.mp4";
const path2 : string = "/static/test-video/10second-demo.mp4";
uni.shareWithSystem({
videoPaths: [path1, path2],
type:'video',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePubMedias() {
uni.hideToast()
uni.chooseVideo({
success(res) {
uni.shareWithSystem({
videoPaths: [res.tempFilePath],
type:'video',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
}
})
},
shareText() {
uni.hideToast()
uni.shareWithSystem({
summary: this.summary,
type:'text',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
shareLink() {
uni.hideToast()
uni.shareWithSystem({
type:'text',
href: 'https://uniapp.dcloud.io',
success(res) {
console.log('Shared----------------------------success')
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateImg() {
uni.hideToast()
const imageSrc : string = "/static/test-image/logo.gif";
uni.shareWithSystem({
type:'image',
imageUrl: imageSrc,
success(res) {
console.log('Shared----------------------------success')
// 分享完成,请注意此时不一定是成功分享
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
// 分享失败
},
complete(res) {
}
})
},
sharePrivateErrorImg() {
uni.hideToast()
const imageSrc : string = "/static/test-image/logo.jpg11";
uni.shareWithSystem({
imageUrl: imageSrc,
type:'image',
success(res) {
console.log('Shared----------------------------success')
// 分享完成,请注意此时不一定是成功分享
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
// 分享失败
},
complete(res) {
}
})
},
sharePrivateImgs() {
uni.hideToast()
const errorImageSrc1 : string = "/static/test-image/logo.gif";
const errorImageSrc2 : string = "/static/test-image/logo.png";
const imageSrc : string = "/static/test-image/logo.jpg";
let imageUrlList : string[] = Array()
imageUrlList.push(errorImageSrc2)
imageUrlList.push(imageSrc)
// imageUrlList.push(errorImageSrc1)
uni.shareWithSystem({
imagePaths: imageUrlList,
type:'image',
success(res) {
console.log('Shared----------------------------success')
// 分享完成,请注意此时不一定是成功分享
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePrivateErrorImgs() {
uni.hideToast()
const errorImageSrc1 : string = "/static/test-image/logo.jpg1";
const errorImageSrc2 : string = "/static/test-image/logo.jpg3";
const imageSrc : string = "/static/test-image/logo.jpg";
let imageUrlList : string[] = Array()
imageUrlList.push(imageSrc)
imageUrlList.push(errorImageSrc1)
imageUrlList.push(errorImageSrc2)
uni.shareWithSystem({
imagePaths: imageUrlList,
type:'image',
success(res) {
console.log('Shared----------------------------success')
// 分享完成,请注意此时不一定是成功分享
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
shareAll() {
uni.hideToast()
const imageSrc : string = "/static/test-video/fast-forward.png";
let imageUrlList : string[] = Array()
imageUrlList.push(imageSrc)
uni.shareWithSystem({
summary: this.summary,
href: 'https://uniapp.dcloud.io',
imagePaths: imageUrlList,
type:'image',
success(res) {
console.log('Shared----------------------------success')
// 分享完成,请注意此时不一定是成功分享
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
},
sharePubImg() {
uni.hideToast()
uni.chooseImage({
count: 3,
sourceType: ['camera', 'album'],
success(e) {
console.log(e)
console.log(JSON.stringify(e))
uni.shareWithSystem({
// summary: "aa",
// href: 'https://uniapp.dcloud.io',
imagePaths: e.tempFilePaths,
type:'image',
success(res) {
console.log('Shared----------------------------success')
// 分享完成,请注意此时不一定是成功分享
},
fail(res) {
console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
}
})
}, },
shareSnapShot() { shareSnapShot() {
uni.hideToast()
uni.getElementById("viewshot")?.takeSnapshot( uni.getElementById("viewshot")?.takeSnapshot(
{ {
success: function (res) { success: function (res) {
// 打印截图文件临时路径 uni.shareWithSystem({
console.log(res.tempFilePath) imageUrl: res.tempFilePath,
const context = UTSAndroid.getUniActivity()!; type:'image',
const intent = new Intent(Intent.ACTION_SEND) success(res) {
intent.setType("image/*"); console.log('Shared----------------------------success')
let file = new File(res.tempFilePath) // 分享完成,请注意此时不一定是成功分享
const uri = UTSAndroid.getFileProviderUri(file) //3.99支持 },
intent.putExtra(Intent.EXTRA_STREAM, uri); fail(res) {
context.startActivity(Intent.createChooser(intent, "分享到")); console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
uni.showToast({
icon: "error",
title: "errorCode=" + res.errCode
})
},
complete(res) {
}
})
}, },
fail: function (res) { fail: function (res) {
console.log(res) console.log(res)
...@@ -55,6 +513,8 @@ ...@@ -55,6 +513,8 @@
<style> <style>
.button { .button {
margin: 15px; margin-left: 30px;
margin-right: 30px;
margin-bottom: 15px;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册