HarmonyOS鸿蒙Next中ohos.file.picker无法选取
HarmonyOS鸿蒙Next中ohos.file.picker无法选取 我使用ohos.file.picker选取图片,这是我的代码:
try {
let photoSelectOptions = new picker.PhotoSelectOptions();
photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = 1;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(photoSelectOptions)
.then((photoSelectResult: picker.PhotoSelectResult) => {
console.error('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
})
.catch((err: BusinessError) => {
console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
但是在打开相册的时候没有看到选中按钮,如附件,这是为何? 使用的api12 手机软件版本: next.0.0.71
更多关于HarmonyOS鸿蒙Next中ohos.file.picker无法选取的实战教程也可以访问 https://www.itying.com/category-93-b0.html
photoSelectOptions.maxSelectNumber选择媒体文件的最大数目大于1才会显示选中按钮。
推荐您使用photoAccessHelper
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/photoaccesshelper-photoviewpicker-V5
import { BusinessError } from '@kit.BasicServicesKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(()=>{
try {
const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
photoSelectOptions.maxSelectNumber = 2; // 选择媒体文件的最大数目
let uris: Array<string> = [];
const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
uris = photoSelectResult.photoUris;
console.info('photoViewPicker.select to file succeed and uris are:' + uris);
}).catch((err: BusinessError) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
})
}
.height('100%')
.width('100%')
}
}
打开相册,点击图片,右下角完成。会返回照片的路径 uris
import { BusinessError } from '@kit.BasicServicesKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
@State imgPath: string = ''
build() {
Column() {
Image(this.imgPath).height('50%')
Text(this.message)
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(()=>{
try {
const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE
photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
let uris: Array<string> = [];
const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
uris = photoSelectResult.photoUris;
console.info('photoViewPicker.select to file succeed and uris are:' + uris);
this.imgPath = uris[0]
}).catch((err: BusinessError) => {
console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
})
}
.height('100%')
.width('100%')
}
}
更多关于HarmonyOS鸿蒙Next中ohos.file.picker无法选取的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,ohos.file.picker无法选取文件可能是由于以下原因:
-
权限问题:应用可能未获取到必要的文件访问权限。确保在应用的
config.json文件中正确配置了ohos.permission.READ_USER_STORAGE和ohos.permission.WRITE_USER_STORAGE权限。 -
API使用错误:
ohos.file.picker的API调用可能不符合规范。检查代码中是否正确使用了FilePicker类的相关方法,如selectFile或selectFolder。 -
系统限制:某些系统版本或设备可能对文件选择器有特定的限制或已知问题。确认当前系统版本和设备是否支持
ohos.file.picker的功能。 -
路径问题:文件路径可能不正确或无法访问。确保文件路径符合系统要求,并且文件确实存在于指定路径中。
-
UI未响应:文件选择器的UI可能未正确显示或未响应。检查应用的UI线程是否正常,以及是否正确处理了文件选择器的回调。
-
兼容性问题:应用可能未完全兼容HarmonyOS Next的API。确认应用的目标API级别是否与HarmonyOS Next匹配。
-
日志信息:查看应用的日志信息,查找与
ohos.file.picker相关的错误或警告信息,以便进一步定位问题。 -
系统更新:确保系统和应用都已更新到最新版本,以避免因旧版本导致的兼容性问题。
-
文件类型限制:文件选择器可能对文件类型有特定限制。确认所选文件类型是否在允许的范围内。
-
系统资源不足:系统资源不足可能导致文件选择器无法正常工作。检查设备的内存和存储空间是否充足。
以上是ohos.file.picker无法选取文件的可能原因。
在HarmonyOS鸿蒙Next中,ohos.file.picker是用于文件选择的模块。如果无法选取文件,可能的原因包括:
- 权限问题:确保应用已获取
ohos.permission.READ_MEDIA和ohos.permission.WRITE_MEDIA权限。 - 配置错误:检查
config.json文件中是否正确配置了fileAccess能力。 - API使用错误:确保正确调用
FilePicker.pick()方法,并处理返回的文件URI。 - 系统限制:某些文件类型或路径可能受系统限制,无法选择。
建议检查上述问题,并参考官方文档进行调试。


