[英]getting image from gallery not working in phoneGap
我們需要獲取圖片表格Gallery.So,然后將插件添加到我們的應用中,然后我們嘗試像這樣
var pictureSource; // picture source
var destinationType; // sets the format of returned value
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
alert("ondevicereday");
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
function onPhotoDataSuccess(imageData) {
alert("Open");
var smallImage = document.getElementById('smallImage');
smallImage.src = "data:image/jpeg;base64," + imageData;
}
function capturePhoto(){
alert("justOpen");
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY });
}
function onFail(message) {
alert('Failed because: ' + message);
}
但是我們無法打開ondevicereday
頁面加載“ ondevicereday
”警報時顯示,然后我們單擊openGallery
按鈕,它顯示justOpen
警報,但我們無法打開,所以請指導我們,告訴我代碼中有什么錯誤
我已經使用cordova的File API插件實現了此功能,該File API插件可以讓您訪問Android Phone中的文件系統。
這是鏈接https://github.com/wymsee/cordova-imagePicker
<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
<button style="width:30%;" onclick="pickImage()">Clean Data</button>
<p id="lastPhoto"></p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function pickImage(){
var lastPhotoContainer = document.getElementById("lastPhoto");
var doc= document.getElementBy
window.imagePicker.getPictures(
function(results) {
console.log(results);
for (var i = 0; i < results.length; i++) {
var imageUri=results[i];
console.log('Image URI: ' + results[i]);
lastPhotoContainer.innerHTML = "<img src='" + imageUri + "' style='width: 75%;' />";
}
}, function (error) {
console.log('Error: ' + error);
}
);
}
</script>
</body>
</html>
這個簡單的示例從本地存儲中獲取一張圖像,並將其顯示在應用程序的屏幕上,您可以繼續將圖像從uri轉換為base64並像您一樣使用它。現在,這會將已拍攝的圖片填充到html ..希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.