HarmonyOS 鸿蒙Next 实现点击按钮访问本地相册并选择图片的demo
HarmonyOS 鸿蒙Next 实现点击按钮访问本地相册并选择图片的demo
实现点击按钮可以访问本地相册并能够选择图片的demo
2 回复
参考以下demo:
import { picker } from '[@kit](/user/kit).CoreFileKit';
import photoAccessHelper from '[@ohos](/user/ohos).file.photoAccessHelper';
import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index9 {
[@State](/user/State) selectedUri: string = '';
choosePhoto() {
try {
let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 5;
PhotoSelectOptions.isEditSupported = true;
PhotoSelectOptions.isPhotoTakingSupported = true;
let photoPicker = new picker.PhotoViewPicker()
let uris: Array<string> = [];
photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
uris = PhotoSelectResult.photoUris;
this.selectedUri = uris[0];
}).catch((err: BusinessError) => {
console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
}
}
build() {
Row() {
Column({ space: 30 }) {
Button('选择图片')
.width(200)
.height(30)
.onClick(() => {
this.choosePhoto()
})
Image(this.selectedUri)
.width('95%')
.height(400)
.padding({top:'30xp'})
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
更多关于HarmonyOS 鸿蒙Next 实现点击按钮访问本地相册并选择图片的demo的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,实现点击按钮访问本地相册并选择图片的Demo,可以通过以下步骤实现。假设你已经创建了一个基本的HarmonyOS项目。
- 配置权限:在
config.json
文件中添加访问存储的权限。
"module": {
"package": "com.example.yourapp",
"reqPermissions": [
"ohos.permission.READ_MEDIA",
"ohos.permission.WRITE_MEDIA"
]
}
- 创建UI布局:在
ability_main.xml
中定义一个按钮。
<DirectionalLayout
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:button_select_image"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="选择图片"/>
</DirectionalLayout>
- 处理按钮点击事件:在
MainAbility.java
(或相应的Kotlin文件)中处理按钮点击事件,启动图片选择器。
Button buttonSelectImage = (Button) findComponentById(ResourceTable.Id_button_select_image);
buttonSelectImage.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE_OR_PICK);
startAbilityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
});
- 处理返回结果:在
onActivityResult
方法中处理选择结果。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK) {
// 处理选择的图片
}
}
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html