提交 ee7e62aa 编写于 作者: W wanganxp

完善file示例;补充截图分享示例

上级 70d47a8f
<template> <template>
<scroll-view> <!-- #ifdef APP -->
<view> <scroll-view style="flex: 1">
<text>点击按钮后需在真机运行控制台看执行结果</text> <!-- #endif -->
<button type="primary" @tap="mkdirTest" class="btn-mkdir">创建文件夹{{mkdirFile}}</button> <text>显示简易操作日志,详细日志需真机运行查看</text><button size="mini" @click="log=''">清空日志</button>
<button type="primary" @tap="writeFileTest" class="btn-write-file">写入文件{{writeFile}}</button> <text style="margin: 2px; padding: 2px; border: 1px solid #000000;" :value="log" />
<button type="primary" @tap="readDirTest" class="btn-read-dir">读取文件夹{{readDir}}</button> <button type="primary" @tap="statFileInfoTest" class="btn-stat-file">递归获取目录files的Stats对象{{statFile}}</button>
<button type="primary" @tap="readFileTest" class="btn-read-file">读取文件{{readFile}}</button> <button type="primary" @tap="mkdirTest" class="btn-mkdir">创建文件夹{{mkdirFile}}</button>
<button type="primary" @tap="copyFileTest" class="btn-copy-file">复制文件{{copyFromFile}}到{{copyToFile}}</button> <button type="primary" @tap="writeFileTest" class="btn-write-file">覆盖写入文件{{writeFile}}</button>
<button type="primary" @tap="renameFileTest" class="btn-rename-file">重命名文件{{renameFromFile}}到{{renameToFile}}</button> <button type="primary" @tap="readDirTest" class="btn-read-dir">读取文件夹{{readDir}}</button>
<button type="primary" @tap="accessFileTest" class="btn-access-file">判断文件{{accessFile}}是否存在</button> <button type="primary" @tap="readFileTest" class="btn-read-file">读取文件{{readFile}}</button>
<button type="primary" @tap="getFileInfoTest" class="btn-get-file-info">获取文件信息{{getFileInfoFile}}</button> <button type="primary" @tap="copyFileTest" class="btn-copy-file">复制文件{{copyFromFile}}到{{copyToFile}}</button>
<button type="primary" @tap="statFileInfoTest" class="btn-stat-file">获取文件Stats{{statFile}}</button> <button type="primary" @tap="renameFileTest"
<button type="primary" @tap="unlinkTest" class="btn-unlink-file">删除文件{{unlinkFile}}</button> class="btn-rename-file">重命名文件{{renameFromFile}}到{{renameToFile}}</button>
<button type="primary" @tap="unlinkAllFileTest" class="btn-clear-file">删除文件夹{{rmDirFile}}下的所有文件</button> <button type="primary" @tap="accessFileTest" class="btn-access-file">判断文件{{accessFile}}是否存在</button>
<button type="primary" @tap="rmdirTest" class="btn-remove-dir">删除文件夹{{rmDirFile}}</button> <button type="primary" @tap="getFileInfoTest" class="btn-get-file-info">获取文件信息{{getFileInfoFile}}</button>
</view> <button type="primary" @tap="unlinkTest" class="btn-unlink-file">删除文件{{unlinkFile}}</button>
</scroll-view> <button type="primary" @tap="copyStaticToFilesTest" class="btn-remove-dir">从static目录复制文件到a目录</button>
<button type="primary" @tap="unlinkAllFileTest" class="btn-clear-file">删除文件夹{{rmDirFile}}下的所有文件</button>
<button type="primary" @tap="rmdirTest" class="btn-remove-dir">删除文件夹{{rmDirFile}}</button>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template> </template>
<script> <script>
export default {
export default {
data() {
data() { return {
return { log: "",
fileListSuccess:[] as string[], fileListSuccess: [] as string[],
fileListComplete:[] as string[], fileListComplete: [] as string[],
accessFileRet:'', accessFileRet: '',
lastFailError:UniError("uni-file-manager",1300000,"mock error"), lastFailError: UniError("uni-file-manager", 1300000, "mock error"),
lastCompleteError:UniError("uni-file-manager",1300000,"mock error"), lastCompleteError: UniError("uni-file-manager", 1300000, "mock error"),
readDir:'a', readDir: 'a',
readFileRet:"", readFileRet: "",
writeFileContent:"锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦", writeFileContent: "中文 en.\r\n\t换行",
getFileInfoAlgorithm:"md5", getFileInfoAlgorithm: "md5",
getFileInfoSize:-1, getFileInfoSize: -1,
getFileInfoDigest:"", getFileInfoDigest: "",
unlinkFile:'a/1.txt', unlinkFile: 'a/1.txt',
accessFile:'a/1.txt', accessFile: 'a/1.txt',
writeFile:'a/1.txt', writeFile: 'a/1.txt',
copyFromFile:'a/1.txt', copyFromFile: 'a/1.txt',
copyToFile:'a/2.txt', copyToFile: 'a/2.txt',
renameFromFile:'a/2.txt', renameFromFile: 'a/2.txt',
renameToFile:'a/3.txt', renameToFile: 'a/3.txt',
getFileInfoFile:'a/1.txt', getFileInfoFile: 'a/1.txt',
statFile:'a/1.txt', statFile: '',
rmDirFile:'a', rmDirFile: 'a',
mkdirFile:'a', mkdirFile: 'a',
readFile:'a/1.txt', readFile: 'a/1.txt',
recursiveVal:true, recursiveVal: true,
done: false, done: false,
writeFileEncoding:"utf-8", writeFileEncoding: "utf-8",
readFileEncoding:"utf-8", readFileEncoding: "utf-8",
statsRet:[] as Array<FileStats>, statsRet: [] as Array<FileStats>,
/** /**
* 待测试的全局环境变量 * 待测试的全局环境变量
*/ */
basePath: uni.env.USER_DATA_PATH, basePath: uni.env.USER_DATA_PATH,
copyToBasePath: uni.env.USER_DATA_PATH, copyToBasePath: uni.env.USER_DATA_PATH,
globalTempPath:uni.env.CACHE_PATH, globalTempPath: uni.env.CACHE_PATH,
globalRootPath:uni.env.SANDBOX_PATH, globalRootPath: uni.env.SANDBOX_PATH,
globalInnerRootPath:uni.env.ANDROID_INTERNAL_SANDBOX_PATH, globalInnerRootPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH,
globalAppResourcePath:uni.env.APP_RESOURCE_PATH, globalAppResourcePath: uni.env.APP_RESOURCE_PATH,
globalUserDataPath:uni.env.USER_DATA_PATH globalUserDataPath: uni.env.USER_DATA_PATH
}
} },
},
onLoad() { onLoad() {
}, },
methods: { methods: {
statFileInfoTest:function(e:any){ statFileInfoTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.stat({ fileManager.stat({
path:`${this.basePath}${this.statFile}`, path: `${this.basePath}${this.statFile}`,
recursive:this.recursiveVal, recursive: this.recursiveVal,
success:function(res:StatSuccessResult){ success: function (res : StatSuccessResult) {
console.log('success',res) this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n'
console.log('statFileInfoTest success', res)
this.statsRet = res.stats this.statsRet = res.stats
console.log('this.statsRet',this.statsRet) console.log('this.statsRet', this.statsRet)
}, },
fail:function(res:any){ fail: function (res : any) {
console.log('fail',res) this.log += 'statFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
console.log('statFileInfoTest fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete",res) console.log("statFileInfoTest complete", res)
this.done = true this.done = true
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as StatOptions) } as StatOptions)
}, },
getFileInfoTest:function(e:any){ getFileInfoTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.getFileInfo({ fileManager.getFileInfo({
filePath:`${this.basePath}${this.getFileInfoFile}`, filePath: `${this.basePath}${this.getFileInfoFile}`,
digestAlgorithm:this.getFileInfoAlgorithm, digestAlgorithm: this.getFileInfoAlgorithm,
success:function(res:GetFileInfoSuccessResult){ success: function (res : GetFileInfoSuccessResult) {
console.log('success',res) this.log += 'getFileInfoTest success:' + JSON.stringify(res) + '\n\n'
console.log('success', res)
this.getFileInfoSize = res.size this.getFileInfoSize = res.size
this.getFileInfoDigest = res.digest this.getFileInfoDigest = res.digest
}, },
fail:function(res:any){ fail: function (res : any) {
console.log('fail',res) this.log += 'getFileInfoTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as GetFileInfoOptions) } as GetFileInfoOptions)
}, },
copyFileTest:function(e:any){ copyFileTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.copyFile({ fileManager.copyFile({
srcPath:`${this.basePath}${this.copyFromFile}`, srcPath: `${this.basePath}${this.copyFromFile}`,
destPath:`${this.copyToBasePath}${this.copyToFile}`, destPath: `${this.copyToBasePath}${this.copyToFile}`,
success:function(res:FileManagerSuccessResult){ success: function (res : FileManagerSuccessResult) {
console.log('success',res) this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
}, console.log('success', res)
fail:function(res:any){ },
console.log('fail',res) fail: function (res : any) {
this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as CopyFileOptions) } as CopyFileOptions)
}, },
renameFileTest:function(e:any){ renameFileTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.rename({ fileManager.rename({
oldPath:`${this.basePath}${this.renameFromFile}`, oldPath: `${this.basePath}${this.renameFromFile}`,
newPath:`${this.basePath}${this.renameToFile}`, newPath: `${this.basePath}${this.renameToFile}`,
success:function(res:FileManagerSuccessResult){ success: function (res : FileManagerSuccessResult) {
console.log('success',res) this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n'
}, console.log('success', res)
fail:function(res:any){ },
console.log('fail',res) fail: function (res : any) {
this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
this.done = true this.done = true
console.log("complete",res) console.log("complete", res)
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as RenameOptions) } as RenameOptions)
}, },
readDirTest:function(e:any){ readDirTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.readdir({ fileManager.readdir({
dirPath:`${this.basePath}${this.readDir}`, dirPath: `${this.basePath}${this.readDir}`,
success:function(res:ReadDirSuccessResult){ success: function (res : ReadDirSuccessResult) {
console.log("success",res) this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n'
console.log("success", res)
this.fileListSuccess = res.files this.fileListSuccess = res.files
}, },
fail:function(res:any){ fail: function (res : any) {
console.log('fail',res) this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
if(res instanceof ReadDirSuccessResult){ if (res instanceof ReadDirSuccessResult) {
this.fileListComplete = res.files this.fileListComplete = res.files
} }
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as ReadDirOptions) } as ReadDirOptions)
}, },
writeFileTest:function(e:any){ writeFileTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.writeFile({ fileManager.writeFile({
filePath:`${this.basePath}${this.writeFile}`, filePath: `${this.basePath}${this.writeFile}`,
data:this.writeFileContent, data: this.writeFileContent,
encoding:this.writeFileEncoding, encoding: this.writeFileEncoding,
success:function(res){ success: function (res) {
console.log('success',res) this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n'
}, console.log('success', res)
fail:function(res){ },
console.log('fail') fail: function (res) {
this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail')
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res){ complete: function (res) {
this.done = true this.done = true
console.log("complete") console.log("complete")
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as WriteFileOptions) } as WriteFileOptions)
}, },
readFileTest:function(e:any){ readFileTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.readFile({ fileManager.readFile({
filePath:`${this.basePath}${this.readFile}`, filePath: `${this.basePath}${this.readFile}`,
encoding:this.readFileEncoding, encoding: this.readFileEncoding,
success:function(res:ReadFileSuccessResult){ success: function (res : ReadFileSuccessResult) {
console.log('success',res) this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n'
console.log('success', res)
this.readFileRet = res.data this.readFileRet = res.data
}, },
fail:function(res:any){ fail: function (res : any) {
console.log('fail',res) this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as ReadFileOptions) } as ReadFileOptions)
}, },
rmdirTest:function(e:any){ rmdirTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.rmdir({ fileManager.rmdir({
dirPath:`${this.basePath}${this.rmDirFile}`, dirPath: `${this.basePath}${this.rmDirFile}`,
recursive:this.recursiveVal, recursive: this.recursiveVal,
success:function(res:FileManagerSuccessResult){ success: function (res : FileManagerSuccessResult) {
console.log('success',res) this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n'
}, console.log('success', res)
fail:function(res:any){ },
console.log('fail',res) fail: function (res : any) {
this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as RmDirOptions) } as RmDirOptions)
}, },
mkdirTest:function(e:any){ mkdirTest: function (e : any) {
// 准备测试数据 // 准备测试数据
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.mkdir({ fileManager.mkdir({
dirPath:`${this.basePath}${this.mkdirFile}`, dirPath: `${this.basePath}${this.mkdirFile}`,
recursive:this.recursiveVal, recursive: this.recursiveVal,
success:function(res:FileManagerSuccessResult){ success: function (res : FileManagerSuccessResult) {
console.log('success',res) this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n'
}, console.log('success', res)
fail:function(res:any){ },
console.log('fail',res) fail: function (res : any) {
this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
this.done = true this.done = true
console.log("complete",res) console.log("complete", res)
} }
} as MkDirOptions) } as MkDirOptions)
}, },
accessFileTest:function(e:any){ accessFileTest: function (e : any) {
this.accessFileRet = '' this.accessFileRet = ''
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.access({ fileManager.access({
path:`${this.basePath}${this.accessFile}`, path: `${this.basePath}${this.accessFile}`,
success:function(res:FileManagerSuccessResult){ success: function (res : FileManagerSuccessResult) {
console.log('success',res) this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n'
console.log('success', res)
this.accessFileRet = res.errMsg this.accessFileRet = res.errMsg
}, },
fail:function(res:UniError){ fail: function (res : UniError) {
console.log('fail',res) this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
} }
} as AccessOptions) } as AccessOptions)
},
unlinkTest:function(e:any){
let fileManager = uni.getFileSystemManager()
fileManager.unlink({ },
filePath:`${this.basePath}${this.unlinkFile}`, unlinkTest: function (e : any) {
success:function(res:FileManagerSuccessResult){
console.log('success',res) let fileManager = uni.getFileSystemManager()
},
fail:function(res:UniError){ fileManager.unlink({
console.log('fail',res) filePath: `${this.basePath}${this.unlinkFile}`,
success: function (res : FileManagerSuccessResult) {
this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n'
console.log('success', res)
},
fail: function (res : UniError) {
this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
console.log("complete",res) console.log("complete", res)
this.done = true this.done = true
} }
} as UnLinkOptions) } as UnLinkOptions)
}, },
unlinkAllFileTest:function(e:any){ unlinkAllFileTest: function (e : any) {
let fileManager = uni.getFileSystemManager() let fileManager = uni.getFileSystemManager()
fileManager.readdir({ fileManager.readdir({
dirPath:`${this.basePath}${this.rmDirFile}`, dirPath: `${this.basePath}${this.rmDirFile}`,
success:function(res:ReadDirSuccessResult){ success: function (res : ReadDirSuccessResult) {
console.log("success to readdir",res) console.log("success to readdir", res)
res.files.forEach(element => { res.files.forEach(element => {
console.log(element) console.log(element)
fileManager.unlink({ fileManager.unlink({
filePath:`${this.basePath}${this.rmDirFile}/${element}`, filePath: `${this.basePath}${this.rmDirFile}/${element}`,
success:function(res:FileManagerSuccessResult){ success: function (res : FileManagerSuccessResult) {
console.log('success unlink',res) this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n'
}, console.log('success unlink', res)
fail:function(res:UniError){ },
console.log('fail unlink',res) fail: function (res : UniError) {
this.lastFailError = res this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
}, console.log('fail unlink', res)
complete:function(res:any){ this.lastFailError = res
if(res instanceof UniError){ },
this.lastCompleteError = res complete: function (res : any) {
} if (res instanceof UniError) {
console.log("complete unlink",res) this.lastCompleteError = res
this.done = true }
} console.log("complete unlink", res)
} as UnLinkOptions) this.done = true
}
} as UnLinkOptions)
}); });
}, },
fail:function(res:any){ fail: function (res : any) {
console.log('fail to readdir',res) this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail to readdir', res)
this.lastFailError = res this.lastFailError = res
}, },
complete:function(res:any){ complete: function (res : any) {
console.log("complete readdir",res) console.log("complete readdir", res)
this.done = true this.done = true
if(res instanceof ReadDirSuccessResult){ if (res instanceof ReadDirSuccessResult) {
this.fileListComplete = res.files this.fileListComplete = res.files
} }
if(res instanceof UniError){ if (res instanceof UniError) {
this.lastCompleteError = res this.lastCompleteError = res
} }
} }
} as ReadDirOptions) } as ReadDirOptions)
}, },
copyStaticToFilesTest: function (e : any) {
}
} let fileManager = uni.getFileSystemManager()
fileManager.copyFile({
srcPath: UTSAndroid.getResourcePath("static/list-mock/mock.json"),
destPath: `${this.copyToBasePath}/a/mock.json`,
success: function (res : FileManagerSuccessResult) {
this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n'
console.log('success', res)
},
fail: function (res : any) {
this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n'
console.log('fail', res)
this.lastFailError = res
},
complete: function (res : any) {
console.log("complete", res)
this.done = true
if (res instanceof UniError) {
this.lastCompleteError = res
}
}
} as CopyFileOptions)
},
}
}
</script> </script>
<style> <style>
......
...@@ -40,10 +40,10 @@ ...@@ -40,10 +40,10 @@
<view class="uni-padding-wrap"> <view class="uni-padding-wrap">
<view class="uni-btn-v"> <view class="uni-btn-v">
<button class="uni-btn btn-getStorageInfoASync" type="primary" @tap="getStorageInfo"> <button class="uni-btn btn-getStorageInfoASync" type="primary" @tap="getStorageInfo">
获取存储概述-异步 获取存储概述信息-异步
</button> </button>
<button class="uni-btn btn-getStorageInfoSync" @tap="getStorageInfoSync"> <button class="uni-btn btn-getStorageInfoSync" @tap="getStorageInfoSync">
获取存储概述-同步 获取存储概述信息-同步
</button> </button>
</view> </view>
<text>{{ storageInfo }}</text> <text>{{ storageInfo }}</text>
......
<template> <template>
<view> <view id="viewshot">
<button class="button" @click="shareText('https://uniapp.dcloud.io/uni-app-x','分享到')">分享</button> <button class="button" @click="shareText('https://uniapp.dcloud.io/uni-app-x','分享到')">分享文本</button>
</view> <button class="button" @click="shareSnapShot">指定view截图并分享</button>
</view>
</template> </template>
<script> <script>
import Intent from 'android.content.Intent'; import Intent from 'android.content.Intent';
import File from 'java.io.File';
export default { export default {
data() { data() {
return { return {
} }
}, },
methods: { methods: {
shareText(text:string,title:string){ shareText(text : string, title : string) {
const context = UTSAndroid.getUniActivity()!; const context = UTSAndroid.getUniActivity()!;
const intent = new Intent(Intent.ACTION_SEND) const intent = new Intent(Intent.ACTION_SEND)
intent.setType("text/plain"); intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text); intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(intent, title)); context.startActivity(Intent.createChooser(intent, title));
// 这里是简单的文本分享示例,如需分享文件图片,需要使用fileProvider // 这里是简单的文本分享示例,如需分享文件图片,需要使用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)
const uri = UTSAndroid.getFileProviderUri(file)
intent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(intent, "分享到"));
},
fail: function (res) {
console.log(res)
uni.showToast({
icon: 'error',
title: '截图失败'
})
}
}
)
},
}
}
</script> </script>
<style> <style>
.button { .button {
margin: 30rpx; margin: 30rpx;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册