HarmonyOS鸿蒙Next中模拟器选择照片并上传无反映
HarmonyOS鸿蒙Next中模拟器选择照片并上传无反映 上传代码用的官方的示例,服务器用的postman,postman已测试可以上传图片,但是用官方的上传代码收不到信息。
代码如下:
import { request } from '@kit.BasicServicesKit';
import { fileIo as fs } from '@kit.CoreFileKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
private openPhotoPicker() {
// Obtain the application file path
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let cacheDir = context.cacheDir;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select({
MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
maxSelectNumber: 1
}, (_, result) => {
if (result) {
result.photoUris.forEach((uri) => {
let file = fs.openSync(uri, fs.OpenMode.CREATE);
// Copy the file to the cache directory
fs.copyFileSync(file.fd, cacheDir + '/test5.jpeg');
this.uploadImage(['internal://cache/test5.jpeg']);
})
}
})
}
private uploadImage(paths: string[]) {
let allFiles = Array<request.File>();
let header = new Map<Object, string>();
header.set('Content-Type', 'multipart/form-data');
header.set('key2', 'value2');
for (let i = 0; i < paths.length; i++) {
allFiles[i] = {
name: 'image' + i + '.jpeg',
filename: 'image' + i + '.jpeg',
uri: paths[i],
type: 'image'
}
}
let data: Array<request.RequestData> = [{ name: 'name', value: 'value' }];
let uploadConfig: request.UploadConfig = {
url: 'https://1cf0ac6c-febe-41db-865a-639a37dac67a.mock.pstmn.io//filetest',
header: header,
method: 'POST',
files: allFiles,
data: data
}
try {
request.uploadFile(this.getUIContext().getHostContext(), uploadConfig, (error, uploadTask) => {
if (uploadTask) {
uploadTask.on('progress', (uploadSize: number, totalSize: number) => {
console.info('progress,uploadedSize:' + uploadSize + ',totalSize:' + totalSize);
})
} else {
console.info('upload failure:' + error);
}
})
} catch (error) {
console.info('upload failure:' + error);
}
}
build() {
Column() {
Button('选择图片上传')
.width('100%')
.onClick(() => {
this.openPhotoPicker();
})
}
.width('100%')
.height('100%')
.padding(16)
.justifyContent(FlexAlign.End)
}
}
更多关于HarmonyOS鸿蒙Next中模拟器选择照片并上传无反映的实战教程也可以访问 https://www.itying.com/category-93-b0.html
一、检测模拟器是否能ping通域名,步骤如下:
1、hec shell
2、ping 1cf0ac6c-febe-41db-865a-639a37dac67a.mock.pstmn.io
二、如果能ping通,麻烦按照如下步骤提供下日志,并提供下应用的包名。
1.hdc shell
2.cd data/log/hilog
3.hilog -w clear (清除多余日志)
4.exit (退出hdc shell)
5.复现问题
6.hdc file recv /data/log/hilog 导出hilog日志
更多关于HarmonyOS鸿蒙Next中模拟器选择照片并上传无反映的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中,模拟器选择照片后无响应,通常与权限配置或模拟器资源文件有关。请检查应用是否已正确声明媒体文件访问权限,并在配置文件中启用相关权限。同时,确保模拟器已加载包含图片的虚拟SD卡或资源文件。若问题持续,可尝试重启模拟器或重新配置模拟器镜像。
从代码来看,问题可能出现在文件路径处理和上传配置上。以下是几个关键点需要检查:
-
文件路径问题:在
openPhotoPicker方法中,你将选中的图片复制到缓存目录,但上传时使用的路径是'internal://cache/test5.jpeg'。在HarmonyOS Next中,上传文件时应使用正确的URI格式。建议直接使用从相册选择器返回的原始URI,或者确保复制后的文件路径能被正确访问。 -
上传配置中的URL:你的URL末尾有双斜杠
//filetest,这可能导致请求路径错误。请检查并修正为正确的URL格式,例如:'https://1cf0ac6c-febe-41db-865a-639a37dac67a.mock.pstmn.io/filetest'。 -
网络权限:确保在
module.json5文件中已声明网络权限。需要添加以下权限请求:"requestPermissions": [ { "name": "ohos.permission.INTERNET" } ] -
模拟器网络:确认模拟器可以正常访问外部网络。尝试在模拟器中打开浏览器访问一个网站,检查网络连接是否正常。
-
错误日志:代码中已经添加了错误日志输出,检查DevEco Studio的Log窗口,查看是否有相关的错误信息输出,特别是
upload failure相关的日志。
建议先修正URL中的双斜杠问题,并检查文件路径是否正确。如果问题仍然存在,请查看日志输出以获取更具体的错误信息。

