Open klren0312 opened 1 year ago
<template>
<view class="content">
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<button @click="download">下载</button>
<button @click="shareFile('com.tencent.mobileqq')">分享</button>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
tempPath: '',
downloadPath: ''
}
},
onLoad() {
},
methods: {
download() {
const Environment = plus.android.importClass('android.os.Environment')
const File = plus.android.importClass('java.io.File')
const downloadUrl = new File(Environment.getExternalStorageDirectory(), 'Download').getAbsolutePath()
const path = downloadUrl + '/testcp.zip'
const fileCheck = new File(path)
if (fileCheck.exists()) {
console.log('delete')
fileCheck.delete()
}
const dtask = plus.downloader.createDownload(
'https://zzes-1251916954.cos.ap-shanghai.myqcloud.com/test.zip',
{
filename: 'file://' + path
},
(res, status) => {
console.log(status)
if (status === 200) {
}
}
)
dtask.start()
},
moveFile(oldfile, newfile) {
try{
const File = plus.android.importClass('java.io.File')
let n = newfile.lastIndexOf('/');
if (n != -1) {
let fileDirs = newfile.substring(0, n)
let directory = new File(fileDirs)
if (!directory.exists()) {
directory.mkdirs(); //不存在创建目录
}
}
let file = new File(oldfile);
let nfile = new File(newfile);
return file.renameTo(nfile);
}catch(e){
console.log(e)
}
return false
},
shareFile(packageName) {
// #ifndef APP-PLUS
return false
// #endif
// #ifdef APP-PLUS
const MainActivity = plus.android.runtimeMainActivity()
const Intent = plus.android.importClass('android.content.Intent')
const Uri = plus.android.importClass('android.net.Uri')
const File = plus.android.importClass('java.io.File')
const MediaScannerConnection = plus.android.importClass('android.media.MediaScannerConnection')
const Environment = plus.android.importClass('android.os.Environment')
const SETTINGS_FILENAME = 'testcp.zip'
const publicDir = new File(Environment.getExternalStorageDirectory(), 'Download').getAbsolutePath()
console.log(publicDir + '/' + SETTINGS_FILENAME)
const listener = new plus.android.implements('android.media.MediaScannerConnection$OnScanCompletedListener', {
'onScanCompleted': (path, uri) => {
try{
const intent = new Intent()
.setAction(Intent.ACTION_SEND)
.setType('application/*')
.setPackage(packageName)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Intent.EXTRA_TEXT, '分享文件')
.putExtra(Intent.EXTRA_STREAM, uri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
MainActivity.startActivity(Intent.createChooser(intent, '分享文件'))
// new File(path).deleteOnExit()
}catch(e){
console.error(e)
}
}
})
console.log(this.downloadPath + SETTINGS_FILENAME)
MediaScannerConnection.scanFile(MainActivity, [publicDir + '/' + SETTINGS_FILENAME], null, listener)
// #endif
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>