Javascript 从图像相册中选择图像时无法读取文件大小
在我的phonegap应用程序(android版本4.4.2)中,我需要从SD卡中选择图像。在这种情况下,我无法读取图像的大小和名称。我的代码是这样的Javascript 从图像相册中选择图像时无法读取文件大小,javascript,android,jquery-mobile,cordova,Javascript,Android,Jquery Mobile,Cordova,在我的phonegap应用程序(android版本4.4.2)中,我需要从SD卡中选择图像。在这种情况下,我无法读取图像的大小和名称。我的代码是这样的 function getSkiImage(id,source){ navigator.camera.getPicture(function(imageURI){ alert(imageURI); window.resolveLocalFileSystemURI(imageURI, function(f
function getSkiImage(id,source){
navigator.camera.getPicture(function(imageURI){
alert(imageURI);
window.resolveLocalFileSystemURI(imageURI, function(fileEntry) {
alert(fileEntry);
fileEntry.file(function(fileObj) {
alert(fileObj.size);
});
});
// tried this also
/*window.requestFileSystem(imageURI, 0, function(data) {
alert(data.size);
}, fail); */
}, fail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: pictureSource.PHOTOLIBRARY });
}
在我的android设备(V4.4.2)中,相册显示如“最近”、“驱动器”、“图像”、“多媒体资料”…当从多媒体资料中选择图像时,只有图像大小得到..除多媒体资料图像大小外,无法获得
参考了这个,但没有成功
在phonegap文档中,他们说:
仅限安卓4.4:安卓4.4引入了一种新的存储访问
使用户更容易浏览和打开文档的框架
跨所有首选的文档存储提供商。科尔多瓦
尚未与此新的存储访问框架完全集成。
因此,getPicture()方法将无法正确返回
用户从“最近”、“驱动器”、“图像”中选择时的图片,
或当destinationType为文件URI时的“外部存储”文件夹。
但是,如果需要,用户将能够正确选择任何图片
他们先通过“Gallery”应用程序。潜在的解决办法
此问题记录在此StackOverflow问题上。请看
CB-5398跟踪此问题
Android使用意图在设备上启动摄像头活动,以
拍摄图像,并在内存不足的手机上使用Cordova活动
可能会被杀。在这种情况下,当
Cordova活动恢复
拍摄照片
var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待PhoneGap与设备连接
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//PhoneGap已准备好使用!
//
函数ondevicerady(){
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
getPhoto(pictureSource.PHOTOLIBRARY);
}
函数onPhotoURISuccess(imageURI){
警报(imageURI);
var largeImage=document.getElementById('largeImage');
largeImage.style.display='block';
largeImage.src=imageURI;
requestFileSystem(LocalFileSystem.PERSISTENT,0,onfileSystemsSuccess,rfail);
resolveLocalFileSystemURI(imageURI、onResolveSuccess、fail);
}
函数rfail(e){
警报(e);
}
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
函数onFileSystemsSuccess(文件系统){
log(fileSystem.name);
}
函数bytesToSize(字节){
变量大小=['Bytes'、'KB'、'MB'、'GB'、'TB'];
如果(字节==0)返回“n/a”;
var i=parseInt(Math.floor(Math.log(字节)/Math.log(1024));
返回Math.round(字节/Math.pow(1024,i),2)+''+大小[i];
};
函数onResolveSuccess(fileEntry){
filenameofajax=fileEntry.name;
var efail=功能(evt){
log(“文件输入错误”+错误代码);
};
var win=函数(文件){
console.log(文件);
for(文件中的变量i){
警报(i+“”+文件[i]);
}
警报(bytesToSize(file.size));
};
fileEntry.file(win,efail);
}
函数efail(e){
警报(“欧空局”)
}
功能失效(e){
警报(“sa”)
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
警报('失败原因:'+消息);
}
拍摄照片
捕获可编辑照片
从照片库
来自相册
检查此项它显示了哪些错误?你看过Cordova上的文件插件了吗@smj2393未显示任何内容…问题,您是在真实设备上测试此功能,还是在emulator上测试此功能?@Gajotres在我的nexus设备v 4.4.2Nice中,现在告诉我您第一次打开菜单(允许您选择图像)时手机显示了哪些图像库?相信我,这是一个重要的问题
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
getPhoto(pictureSource.PHOTOLIBRARY);
}
function onPhotoURISuccess(imageURI) {
alert(imageURI);
var largeImage = document.getElementById('largeImage');
largeImage.style.display = 'block';
largeImage.src = imageURI;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, rfail);
window.resolveLocalFileSystemURI(imageURI, onResolveSuccess, fail);
}
function rfail(e){
alert(e);
}
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
function onFileSystemSuccess(fileSystem) {
console.log(fileSystem.name);
}
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};
function onResolveSuccess(fileEntry) {
filenameofajax=fileEntry.name;
var efail = function(evt) {
console.log("File entry error "+error.code);
};
var win=function(file) {
console.log(file);
for (var i in file){
alert(i+""+file[i]);
}
alert(bytesToSize(file.size));
};
fileEntry.file(win, efail);
}
function efail(e) {
alert("esa")
}
function fail(e) {
alert("sa")
}
// 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>