#鸿蒙通关秘籍#HarmonyOS NETX中如何调用相机拍照或相册功能并上传


#HarmonyOS NEXT
2024-11-26 11:35:54
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
小新是码农
1

参考

import { BusinessError } from '@ohos.base'; import { rcp } from '@kit.RemoteCommunicationKit'; import { picker } from '@kit.CoreFileKit'; import fs from '@ohos.file.fs'; import { http } from '@kit.NetworkKit'; let uploadUrl: string = '​​http://xxxxxxxxx:8080/upload​​';

function example01(): string { let uri = ''; let photoViewPicker = new picker.PhotoViewPicker(); let photoSelectOption = new picker.PhotoSelectOptions(); photoSelectOption.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; photoViewPicker.select(photoSelectOption).then((photoSelectResult) => { console.log("fyh photoSelectResult:" + photoSelectResult); uri = photoSelectResult.photoUris[0]; console.log("fyh uri:" + uri); try { let resultPhoto = fs.openSync(uri,fs.OpenMode.READ_ONLY); let resultName = ​​resultPhoto.name​​; let fileTemp = fs.openSync(getContext().filesDir+​​resultPhoto.name​​,fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE); let imageUri = fileTemp.path; fs.copyFileSync(resultPhoto.fd,fileTemp.fd); fs.closeSync(resultPhoto); fs.closeSync(fileTemp); const httpRequest = http.createHttp(); httpRequest.request(uploadUrl,{ method:http.RequestMethod.POST, header:{ 'Content-Type': 'multipart/form-data', 'Connection':'keep-alive' }, expectDataType:http.HttpDataType.ARRAY_BUFFER, multiFormDataList: [ { name:'file', contentType: 'image/jpg', filePath: imageUri, remoteFileName:'file.jpg' }, ], },(err,data) => { console.log('fyh:上传结束') httpRequest.destroy(); } ) } catch (err) { console.error(​​fyh:Failed to request the upload. err: ${JSON.stringify(err)}​​); }

}).catch((err:BusinessError) => { console.error(​​Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}​​); }) return uri; } function testRcpMultiPartUpload() { example01(); }

@Entry @Component struct Index { @State message: string = 'Hello World';

build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { testRcpMultiPartUpload(); }) } .width('100%') } .height('100%') } }

分享
微博
QQ
微信
回复
2024-11-29 11:54:04
相关问题
如何调用系统相机拍照
1800浏览 • 1回复 待解决
HarmonyOS 相册/拍照 压缩 上传demo
142浏览 • 1回复 待解决
HarmonyOS 相册相机拍照并裁剪
204浏览 • 1回复 待解决
HarmonyOS 调用相机拍照问题
152浏览 • 1回复 待解决
如何调用系统相机拍照、录视频?
487浏览 • 1回复 待解决
HarmonyOS 相机相册功能开发
155浏览 • 1回复 待解决
harmonyOS基于api9如何调用相机拍照
4013浏览 • 1回复 待解决
进入相册拍照选择图片做头像
13730浏览 • 2回复 已解决