移动浏览器模拟器从相册获取照片在MobileFirst中不起作用

移动浏览器模拟器从相册获取照片在MobileFirst中不起作用

本文介绍了使用移动浏览器模拟器从相册获取照片在MobileFirst中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从phonegap运行此代码,以便将其集成到使用IBM MobileFirst构建的混合android应用程序中.

I am trying to run this code from phonegap so I can integrate it in my hybrid android application built with IBM MobileFirst.

拍摄照片"按钮可以正常工作(来源:相机)

The capture photo button is working perfectly(source: camera)

其他按钮根本不起作用..我希望能够从我的画廊或相册中获取照片...

While the other buttons are not working at all.. I want to be able to get photo from my gallery or album...

 <!DOCTYPE html>
<html>
  <head>
<title>Capture Photo</title>

<script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
<script type="text/javascript" charset="utf-8">

var pictureSource;   // picture source
var destinationType; // sets the format of returned value

// Wait for Cordova to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);

// Cordova is ready to be used!
//
function onDeviceReady() {
    pictureSource=navigator.camera.PictureSourceType;
    destinationType=navigator.camera.DestinationType;
}

// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
  // Uncomment to view the base64 encoded image data
  // console.log(imageData);

  // Get image handle
  //
  var smallImage = document.getElementById('smallImage');

  // Unhide image elements
  //
  smallImage.style.display = 'block';

  // Show the captured photo
  // The inline CSS rules are used to resize the image
  //
  smallImage.src = "data:image/jpeg;base64," + imageData;
}

// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
  // Uncomment to view the image file URI
  // console.log(imageURI);

  // Get image handle
  //
  var largeImage = document.getElementById('largeImage');

  // Unhide image elements
  //
  largeImage.style.display = 'block';

  // Show the captured photo
  // The inline CSS rules are used to resize the image
  //
  largeImage.src = imageURI;
}

// A button will call this function
//
function capturePhoto() {
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
    destinationType: destinationType.DATA_URL });
}

// A button will call this function
//
function capturePhotoEdit() {
  // Take picture using device camera, allow edit, and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
    destinationType: destinationType.DATA_URL });
}

// A button will call this function
//
function getPhoto(source) {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}

// Called if something bad happens.
//
function onFail(message) {
  alert('Failed because: ' + message);
}

</script>
  </head>
  <body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>

推荐答案

问题中的HTML并非基于MobileFirst Platform Foundation生成的Hybrid应用.它缺少所有必需的参考.对此代码对您完全没有运行,我并不感到惊讶.

The HTML in the question is not based on a Hybrid app generated by MobileFirst Platform Foundation; it is missing all sort of references that are required. I am not surprised this code does not run at all for you.

在MFP Studio中创建MobileFirst应用.该应用程序将内置Cordova.接下来,您需要做的就是添加与Cordova相机API相关的所需代码段:

Create a MobileFirst app in MFP Studio. This app will have Cordova built-in. All you need to do next is to add the required code snippets related to the camera API from Cordova:

func wlCommonInit() {
    pictureSource=navigator.camera.PictureSourceType;
    destinationType=navigator.camera.DestinationType;
}

function onPhotoDataSuccess(imageData) {
   ...
   ...

HTML应放置在应用程序的index.html中.
还要在实际设备中进行测试.根据您在评论中所写的内容-它将在那里工作.

The HTML should be placed in index.html of the application.
Also test in an actual device. Per the written by you in the comments - it will then work there.

这篇关于使用移动浏览器模拟器从相册获取照片在MobileFirst中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:28