问题
In my phonegap application(android version 4.4.2) i need to pick image form sdcard. In this i am unable to read the image size and name. My code is like.
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 });
}
In my android device(v 4.4.2) album shows like "Recent", "Drive", "Images","Gallery",...when select image from gallery then only the image size is getting..other than Gallery image size is not able to get..
refered this but not get success
Cordova/PhoneGap Photo File Size
In phonegap doc they say :
Android 4.4 only: Android 4.4 introduced a new Storage Access Framework that makes it easier for users to browse and open documents across all of their preferred document storage providers. Cordova has not yet been fully integrated with this new Storage Access Framework. Because of this, the getPicture() method will not correctly return pictures when the user selects from the "Recent", "Drive", "Images", or "External Storage" folders when the destinationType is FILE_URI. However, the user will be able to correctly select any pictures if they go through the "Gallery" app first. Potential workarounds for this issue are documented on this StackOverflow question. Please see CB-5398 to track this issue.
Android uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. In this scenario, the image may not appear when the Cordova activity is restored.
回答1:
<!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>
check this
来源:https://stackoverflow.com/questions/23736419/file-size-not-able-to-read-when-images-is-choosed-from-image-album