选择图像从Android图像库 - PhoneGap不工作 [英] choose image from android image gallery - PhoneGap not working

查看:142
本文介绍了选择图像从Android图像库 - PhoneGap不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Android PhoneGap 2.8.0:



我需要从手机图库中获取图片,需要在应用程序中插入...



我的代码:



HEAD:

 < meta name =viewportcontent =width = device-width,initial-scale = 1/> 
< link rel =stylesheethref =css / jquery.mobile-1.0rc1.min.css/>
< link rel =stylesheethref =css / styles.css/>

< script src =js / jquery.js>< / script>
< script src =js / jquery.mobile-1.0rc1.min.js>< / script>
< script src =js / cordova.js>< / script>

HTML:

 < button data-theme =donclick =getPhoto(pictureSource.PHOTOLIBRARY);>浏览照片< / button& 

< div id =photos>< / div>

SCRIPT

  function onPhotoURISuccess(imageURI){

var img = $('< img />')
img.attr('src',imageURI);


img.appendTo('#photos');

}

但它不工作....这个?



错误,我在控制台



file:/// android_asser / www / index.html:未捕获的类型错误:无法读取未定义的属性'PHOTOLIBRARY'



未捕获的类型错误:[Object Object]在file:///android_asset/www/js/jquery.mobile-1.0rc1.min.js



之后,我删除了jquery和jquery-mobile

解决方案


$ b

我实现了这段代码...最近...请检查

 < script type =text / javascriptcharset =utf-8src =cordova.js>< / script> 
< script type =text / javascriptcharset =utf-8>

var pictureSource;
var destinationType;

document.addEventListener(deviceready,onDeviceReady,false);

function onDeviceReady()
{
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
}


function onPhotoURISuccess(imageURI)
{
console.log(imageURI);
var largeImage = document.getElementById('largeImage');
largeImage.style.display ='block';
largeImage.src = imageURI;
}

function onPhotoDataSuccess(imageURI)
{
var imgProfile = document.getElementById('imgProfile');
imgProfile.src = imageURI;
if(sessionStorage.isprofileimage == 1)
{
getLocation();
}
movePic(imageURI);
}

function onFail(message)
{
alert('Failed because:'+ message);
}

function movePic(file)
{
window.resolveLocalFileSystemURI(file,resolveOnSuccess,resOnError);
}

function resolveOnSuccess(entry)
{
var d = new Date();
var n = d.getTime();
var newFileName = n +.jpg;
var myFolderApp =MyAppFolder;
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,function(fileSys)
{
fileSys.root.getDirectory(myFolderApp,
{create:true,exclusive:false},
function(directory)
{
entry.moveTo(directory,newFileName,successMove,resOnError);
},
resOnError);
},
resOnError);
}

function successMove(entry)
{
sessionStorage.setItem('imagepath',entry.fullPath);
}

function resOnError(error)
{
alert(error.code);
}

function capturePhotoEdit()
{
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true,
destinationType :destinationType.DATA_URL});
}

function getPhoto(source)
{
navigator.camera.getPicture(onPhotoURISuccess,onFail,{quality:50,
destinationType:destinationType。 FILE_URI,
sourceType:source});
}



< / script>

我的html

 < button onclick =capturePhoto();>捕获照片< / button> 
< button onclick =getPhoto(pictureSource.PHOTOLIBRARY);>来自图库!!< / button>


For Android in PhoneGap 2.8.0:

I need to get image from phone gallery and need to insert in app...

My code:

HEAD:

<meta name="viewport" content="width=device-width,initial-scale=1"/>
<link rel="stylesheet" href="css/jquery.mobile-1.0rc1.min.css" />
<link rel="stylesheet" href="css/styles.css" />

<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.0rc1.min.js"></script>
<script src="js/cordova.js"></script>

HTML:

<button data-theme="d" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Browse    Photos</button>

<div id="photos"></div>

SCRIPT:

function onPhotoURISuccess(imageURI) {

var img = $('<img />');
img.attr('src', imageURI);


img.appendTo('#photos');

}

But its not working....how can i do this?

error that i got in console

file:///android_asser/www/index.html: Uncaught typeerror : cannot read property 'PHOTOLIBRARY' of undefined

Uncaught typeerror : [Object Object] has no method 'split' at file:///android_asset/www/js/jquery.mobile-1.0rc1.min.js

after i removed the jquery and jquery-mobile its working... any problem with jQuery with phonegap?

Thanks in advance

解决方案

I have implemented this code...recently..check this

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

var pictureSource;   
var destinationType; 

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() 
{
    pictureSource=navigator.camera.PictureSourceType;
    destinationType=navigator.camera.DestinationType;
}


function onPhotoURISuccess(imageURI) 
{
    console.log(imageURI);
    var largeImage = document.getElementById('largeImage');
    largeImage.style.display = 'block';
    largeImage.src = imageURI;
}

function onPhotoDataSuccess(imageURI) 
{ 
    var imgProfile = document.getElementById('imgProfile');
    imgProfile.src = imageURI;
    if(sessionStorage.isprofileimage==1)
    {
        getLocation();
    }
    movePic(imageURI);
}

function onFail(message) 
{
    alert('Failed because: ' + message);
}

function movePic(file)
{ 
    window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); 
} 

function resolveOnSuccess(entry)
{ 
    var d = new Date();
    var n = d.getTime();
    var newFileName = n + ".jpg";
    var myFolderApp = "MyAppFolder";
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) 
    {      
        fileSys.root.getDirectory( myFolderApp,
                {create:true, exclusive: false},
                function(directory) 
                {
                    entry.moveTo(directory, newFileName,  successMove, resOnError);
                },
        resOnError);
    },
    resOnError);
}

function successMove(entry) 
{
    sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError(error) 
{
    alert(error.code);
}

function capturePhotoEdit() 
{
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
    destinationType: destinationType.DATA_URL });
}

function getPhoto(source) 
{
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}



</script>

My html

<button onclick="capturePhoto();">Capture Photo</button>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Gallery!!</button>

这篇关于选择图像从Android图像库 - PhoneGap不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆