HarmonyOS 如何在h5页面直接拉起系统相册?

<input type="file" id="imageInput" accept="image/*" capture="camera">
HarmonyOS
2024-11-07 08:43:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

参考如下:

<!DOCTYPE html> 
  <html> 
  <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8"> 
  <script> 
  function myFunction(mystr) 
  { 
    var img = new Image(); 
    img.width = 100; 
    img.height = 50; 
    img.src = "data:image/png;base64,"+mystr; 
    document.body.appendChild(img); 
    <!-- let str=objName.test()--> 
  console.log(mystr) 
    console.log("这部分是将arkts发送过来的str渲染到h5页面上,或者存在h5侧的变量中") 
  } 
window.onload = function() { 
  // 获取文件输入元素 
  var fileInput = document.getElementById('upload'); 
 
  // 监听文件输入的改变事件 
  fileInput.addEventListener('change', function(e) { 
    // 获取选中的文件 
    var file = e.target.files[0]; 
    // 创建一个FileReader对象 
    var reader = new FileReader(); 
 
    // 定义文件读取完成后的回调函数 
    reader.onloadend = function() { 
      // 获取图片元素 
      var img = document.getElementById('preview'); 
      // 将读取到的结果赋值给图片元素的src属性 
      img.src = reader.result; 
    } 
 
    // 读取文件内容 
    if (file) { 
      reader.readAsDataURL(file); 
    } 
  }); 
} 
 
  </script> 
  </head> 
  <body> 
  <form id="upload-form" enctype="multipart/form-data"> 
  <!-- 修改type属性为camera --> 
  <input type="file" id="upload" name="upload" accept="image/*" capture="camera" /> 
  <!-- 添加一个图片元素用于展示图片 --> 
  <img id="preview"/> 
  </form> 
  <div id="155134"></div> 
  </body> 
  </html>
Web({ src: $rawfile('TestonShowFileSelector.html'), controller: this.controller }) 
  .javaScriptAccess(true) 
  .domStorageAccess(true) 
  .onShowFileSelector((event) => { 
    // //-------调用相册------------------------- 
    console.log('MyFileUploader onShowFileSelector invoked') 
    const photoSelectOptions = new picker.PhotoSelectOptions(); 
    // 过滤选择媒体文件类型为IMAGE_VIDEO_TYPE 
    photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE; 
    // 选择媒体文件的最大数目 
    photoSelectOptions.maxSelectNumber = 2; 
    //let uris: Array<string> = []; 
    const photoViewPicker = new picker.PhotoViewPicker(); 
    photoViewPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => { 
      this.uris = photoSelectResult.photoUris; 
      let file1 = fs.openSync(this.uris[0]) 
      let array:ArrayBuffer = new ArrayBuffer(fs.statSync(file1.fd).size) 
      fs.readSync(file1.fd,array) 
      let unit = new Uint8Array(array) 
      let base64 = new util.Base64Helper(); 
      let mystr = base64.encodeToStringSync(unit); 
      this.base64Str = "myFunction(\"" + mystr + "\")" 
      this.controller.runJavaScript(this.base64Str) 
      console.info('photoViewPicker.select to file succeed and uris are:' + this.uris); 
    }).catch((err: BusinessError) => { 
      console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 
    }) 
 
    return false 
  }) 
 
} 
} 
}
分享
微博
QQ
微信
回复
2024-11-07 17:29:49
相关问题
如何在HarmonyOS中调试h5页面
938浏览 • 1回复 待解决
HarmonyOS h5页面缩放问题
569浏览 • 0回复 待解决
HarmonyOS H5页面加载缓存机制
285浏览 • 1回复 待解决
如何使H5页面适配多设备?
766浏览 • 1回复 待解决
HarmonyOS Web组件加载在线H5页面
227浏览 • 1回复 待解决
HarmonyOS h5页面是否可以适配Harmony OS
529浏览 • 1回复 待解决
HarmonyOS h5拉起系统扫码页面的demo
226浏览 • 1回复 待解决
升级API11后h5页面字体变小了
1890浏览 • 1回复 待解决