回答
在 Hamibot 脚本中提交图片文件,通常是指通过 HTTP 请求将图片文件上传到服务器。你可以先用 images 模块获取或读取图片文件,再使用 hamibot.http 模块的 postMultipart 方法上传。
下面是完整示例,假设你已有一张截图要上传:
// 文档地址 https://docs.hamibot.com/
// 请求截图权限
if (!requestScreenCapture()) {
toastLog('没有授予 Hamibot 屏幕截图权限');
hamibot.exit();
}
sleep(1000);
// 截图并保存到本地文件
var img = captureScreen();
var filePath = '/storage/emulated/0/screenshot.png';
images.save(img, filePath, 'png', 100);
// 读取该文件,准备上传
var file = images.read(filePath);
// 上传到接口(示例使用 multipart/form-data)
var res = http.postMultipart('https://example.com/upload', {
file: {
fileName: 'screenshot.png',
data: file,
contentType: 'image/png'
}
});
// 处理响应
toastLog('上传结果:' + res.body.string());
hamibot.exit();
关键点说明:
- 使用
images.save()将图片对象保存为文件,方便后续读取。 - 使用
http.postMultipart()时,data字段可以是images.read()返回的图片对象,也可以是文件路径的绝对路径字符串。 - 如果图片已存在于存储中,可以直接用文件路径上传,无需先读取:
http.postMultipart('https://example.com/upload', { file: { fileName: 'photo.jpg', data: '/storage/emulated/0/DCIM/photo.jpg', contentType: 'image/jpeg' } }); - 注意申请存储权限,并在脚本开头用
auto.waitFor()确保无障碍服务已启动(如果需要截图权限)。
如果你需要上传的是其他图片来源(如从相册选择),可以结合 media.scanFile 或直接读取已知路径。
ℹ️ 内容由 AI 生成,可能不准确,请注意核实