HarmonyOS H5如何访问相册?

HarmonyOS H5如何访问相册?

HarmonyOS
2024-11-05 10:39:14
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

​此demo用于当web组件加载一个h5页面时,用与h5页面用input标签调用手机相册以及系统相机。

注入JavaScript对象到window对象中,并在window对象中调用该对象的方法。注册后,须调用refresh接口生效。

注册对象的名称,与window中调用的对象名一致。注册后window对象可以通过此名字访问应用侧JavaScript对象。​

import business_error from '@ohos.base'; 
import picker from '@ohos.file.picker'; 
import { BusinessError } from '@ohos.base'; 
import common from '@ohos.app.ability.common'; 
import web_webview from '@ohos.web.webview'; 
 
class pickImage { 
  constructor() { 
  } 
 
  testPickImage(): string { 
    try { 
 
      let PhotoSelectOptions = new picker.PhotoSelectOptions(); 
      PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; 
      PhotoSelectOptions.maxSelectNumber = 5; 
      let photoPicker = new picker.PhotoViewPicker(); 
      photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => { 
        console.info('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)); 
    } 
    return '121'; 
 
  } 
 
} 
 
class cameraImage { 
  constructor() { 
  } 
 
  testCameraImage(): string { 
    const context = getContext(this) as common.UIAbilityContext 
    context.startAbilityForResult({ 
      action:"ohos.want.action.imageCapture", 
      parameters:{ 
        callBundleName:"com.hm.imageshow" 
      } 
    },(err,data) => { 
      console.info("imageCapture:" + `${JSON.stringify(data)}`) 
    }) 
    return '1221'; 
  } 
 
} 
 
@Entry 
@Component 
struct photo { 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
  @State webPickImage: pickImage = new pickImage(); 
  @State webCameraImage: cameraImage = new cameraImage(); 
 
  aboutToAppear(){ 
    // 配置Web开启调试模式 
    web_webview.WebviewController.setWebDebuggingAccess(true); 
  } 
 
  build() { 
    Column() { 
      Button('refresh') 
        .onClick(() => { 
          try { 
            this.controller.refresh(); 
          } catch (error) { 
            let e: business_error.BusinessError = error as business_error.BusinessError; 
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`); 
          } 
        }) 
      Button('Register JavaScript To Window') 
        .onClick(() => { 
          try { 
            this.controller.registerJavaScriptProxy(this.webPickImage, "pickImageName", ["testPickImage"]); 
            this.controller.registerJavaScriptProxy(this.webCameraImage, "cameraImageName", ["testCameraImage"]); 
          } catch (error) { 
            let e: business_error.BusinessError = error as business_error.BusinessError; 
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`); 
          } 
        }) 
      Web({ src: $rawfile('Test1128.html'), controller: this.controller }) 
        .javaScriptAccess(true) 
    } 
  } 
}

html:

<!DOCTYPE html> 
  <html> 
  <body> 
  <button type="button" onclick="pickImage()">唤起图库</button> 
  <button type="button" onclick="cameraImage()">唤起照相机</button> 
  <p id="demo"></p> 
  <script> 
  function pickImage() { 
    let str=pickImageName.testPickImage(); 
  } 
function cameraImage(){ 
  let str2=cameraImageName.testCameraImage(); 
} 
</script> 
  </body> 
  </html>
分享
微博
QQ
微信
回复
2024-11-05 16:33:19
相关问题
HarmonyOS web组件加载h5h5拉起摄像头
463浏览 • 1回复 待解决
HarmonyOS webview h5的localstorage
321浏览 • 1回复 待解决
HarmonyOS 折叠屏H5适配问题
612浏览 • 1回复 待解决
HarmonyOS web原生和H5如何交互?
430浏览 • 1回复 待解决
HarmonyOS H5代码如何复用?
79浏览 • 1回复 待解决
HarmonyOS web与H5交互
522浏览 • 1回复 待解决
HarmonyOS h5和ArkTS通信
114浏览 • 1回复 待解决
如何HarmonyOS中调试h5页面
791浏览 • 1回复 待解决
HarmonyOS 应用涉及H5代码,如何使用?
124浏览 • 1回复 待解决
HarmonyOS h5页面缩放问题
411浏览 • 0回复 待解决
H5页面如何与ArkTS交互
2922浏览 • 1回复 待解决
h5如何在鸿蒙设备进行调试
1506浏览 • 1回复 待解决
如何实现H5自定义事件
2203浏览 • 1回复 待解决
HarmonyOSH5如何跳转到应用市场
596浏览 • 1回复 待解决
webview加载Vue h5失败
17654浏览 • 5回复 待解决
HarmonyOS H5和应用侧数据交互
182浏览 • 1回复 待解决
HarmonyOS web通过loadData加载h5标签
107浏览 • 1回复 待解决