HarmonyOS 鸿蒙Next 拉起系统分享框失败
HarmonyOS 鸿蒙Next 拉起系统分享框失败
鸿蒙官方flutter的exit(0)方法行为不正确,APP调用这个方法退出flutter APP,而flutter内部却把这个退出APP的行为记录为cppcrash异常情况(android和iOS则没这个问题,请改造)。具体请看附件的日志文件
2 回复
可能原因: 使用API不当,API抛出了异常,导致无法拉起。
其他模块出现异常,导致无法拉起。
处理步骤:
-
首先排查是否是分享API使用不当导致抛出异常,具体参考分享服务API和错误码,找到问题后修改代码。
-
如果是其他模块出现异常,查看是否有faultlog,有的话可以根据faultlog中的模块排查问题。
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/share-system-share-V5
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/share-error-code-V5
请参考以下demo,添加网络权限后即可运行:
import { common } from '@kit.AbilityKit';
import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { BusinessError, request } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
import { fileUri } from '@kit.CoreFileKit';
@Entry
@Component
struct Index {
@State imageUri: string = 'https://inews.gtimg.com/om_bt/OM7DU9LBs8fQ2NuR3yTefk9cTN0gGGEoL9dL7AOANr8ecAA/641';
context = getContext(this) as common.UIAbilityContext;
filePath = this.context.filesDir + '/test.jpg';
onPageShow(): void {
this.checkImageExist()
}
checkImageExist(notExistCallback?: Function) {
fs.access(this.filePath).then((res: boolean) => {
if (res) {
console.info("TestShare file exists");
this.imageUri = fileUri.getUriFromPath(this.filePath);
} else {
console.info("TestShare file not exists");
if (notExistCallback) {
notExistCallback()
}
}
}).catch((err: BusinessError) => {
console.info("TestShare access failed with error message: " + err.message + ", error code: " + err.code);
});
}
// 只是为了分享图片,随便找个图片下载
downloadFile(callback: Function) {
this.checkImageExist(() => {
request.downloadFile(this.context, {
url: 'https://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2019-11-22%2F5dd7afa7df0fc.jpg&thumburl=https%3A%2F%2Fimg1.baidu.com%2Fit%2Fu%3D2205810988%2C4283060315%26fm%3D253%26fmt%3Dauto%26app%3D138%26f%3DJPEG%3Fw%3D800%26h%3D500',
filePath: this.filePath,
background: true
}).then((downloadTask: request.DownloadTask) => {
downloadTask.on('progress', (receivedSize: number, totalSize: number) => {
console.info("TestShare download receivedSize:" + receivedSize + " totalSize:" + totalSize);
});
downloadTask.on('complete', () => {
console.info('TestShare download complete');
let file = fs.openSync(this.filePath, fs.OpenMode.READ_WRITE);
console.info('TestShare The content of file: %{public}s', 'file:/' + this.filePath);
let arrayBuffer = new ArrayBuffer(1024);
let readLen = fs.readSync(file.fd, arrayBuffer);
let buf: buffer.Buffer = buffer.from(arrayBuffer, 0, readLen);
console.info('TestShare The content of file: %{public}s', buf.toString());
fs.closeSync(file);
callback()
})
}).catch((err: BusinessError) => {
console.error(`TestShare Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`);
});
})
}
TestSystemShare() {
// 构造ShareData,需配置一条有效数据信息
let data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.PLAIN_TEXT,
content: 'Hello HarmonyOS'
});
// 获取文件的沙箱路径
let pathInSandbox = this.context.filesDir + '/test.jpg';
// 将沙箱路径转换为uri
let uri = fileUri.getUriFromPath(pathInSandbox);
// 添加多条记录
data.addRecord({
utd: utd.UniformDataType.PNG,
uri: uri
});
// 构建ShareController
let controller: systemShare.ShareController = new systemShare.ShareController(data);
// 注册分享面板关闭监听
controller.on('dismiss', () => {
console.log('TestShare Share panel closed');
// 分享结束,可处理其他业务。
});
// 进行分享面板显示
controller.show(this.context, {
previewMode: systemShare.SharePreviewMode.DETAIL,
selectionMode: systemShare.SelectionMode.SINGLE
});
}
build() {
Column({space: 10}) {
Button('查看并下载图片')
.onClick(() => {
if (this.imageUri !== '') {
return;
}
this.downloadFile(() => {
this.imageUri = fileUri.getUriFromPath(this.filePath);
})
})
Image(this.imageUri)
.width(200)
.height(200)
.backgroundColor(Color.Gray)
Button('系统分享')
.onClick(() => {
this.TestSystemShare()
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}
更多关于HarmonyOS 鸿蒙Next 拉起系统分享框失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html