var pictureSource; // picture source
var destinationType; // sets the format of returned value
var photoid=window.localStorage.getItem("photoid");
var photoData=null;
// 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;
}
// A button will call this function
//
function getPhoto(source) {
alert("Entered sd card");
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
function onPhotoDataSuccess(imageData) {
console.log(imageData);
// Get image handle
var smallImage = document.getElementById('photos');
// 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;
alert(imageData);
photoData = imageData;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("/sdcard/external_sd/"+photoid+".jpg", null, gotFileEntry, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
alert("write success");
};
writer.write(photoData);
}
function fail(error) {
alert(error.code);
}
/* function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
console.log(imageURI);
alert("photo captured");
uploadPhoto(imageURI);
} */
/* function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType,
sourceType: source });
} */
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
I have used the above code to access data in the sd card. But now what i need to do is, get the path of the images present there and put it in a diff object that can access the path and display those images. I have no clue on how to go about that.
Any help is appreciated.
What you can do is write a phonegap plugin for the platform you are developing for. I'll assume it's android. Writing android plugins.
When you call the Phonegap.exec to call the plugin, the plugin, gets the sd card path through
Environment.getExternalStorageDirectory().getAbsolutePath()
and then does a basic search to get all the .jpg and .png files and return a json of all the paths of the files.
Related
I am using a Cordova file plugin to create a folder in the file system of devices. This is working.
My issue is I need to move the audio files to the created directory. How can I achieve that?
Below is my code:
function FeedVoicesave(callback, appendData) {
OnCallback = callback;
OnAppendData = appendData;
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, FeedVoicefail);
}
function gotFS(fileSystem) {
// create new folder if not exist
fileSystem.root.getDirectory("Myfolder", { create: true }, gotDir);
}
// I have audio file myaudio, i need to move myaudio to Myfolder
function gotDir(dirEntry) {
dirEntry.getFile(myaudio,null, OnGetFeedVoiceFile);
}
I am using phonegap to build my app, In the app i am building i have a camera which turns on when i open the app and i see camera controls. once i capture the picture the captured picture should get set as my app background image. how can i do that?
here's what i have tried:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
encodingType: Camera.EncodingType.JPEG,
saveToPhotoAlbum: true,
showOverlay: false
});
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
alert(image.src);
document.body.style.backgroundImage = "url(image.src)";
}
function onFail(message) {
/*alert("you are closing the camera")*/
}
}
Your onSuccess function should look like this
function onSuccess(imageData) {
var image = "data:image/jpeg;base64," + imageData;
document.body.style.backgroundImage = "url(" + image + ")";
}
I'm using the phonegap file upload to upload files to a server. When i select a portrait photo from the gallery it rotates to landscape
function selectPicture() {
navigator.camera.getPicture(
function(uri) {
var img = document.getElementById('camera_image');
img.style.visibility = "visible";
img.style.display = "block";
img.src = uri;
document.getElementById('camera_status').innerHTML = "Success";
},
function(e) {
console.log("Error getting picture: " + e);
document.getElementById('camera_status').innerHTML = "Error getting picture.";
},
{ quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI,correctOrientation : true, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
};
there is a "correctOrientation" parameter for getPicture (http://docs.phonegap.com/en/edge/cordova_camera_camera.md.html) try setting it to false (or true :D)
Hope this helps
I am workign with PhoneGap 3.0.0 and my confic.xml file does have the needed plugins. I can start saying that my sound do start when i run my code, and that everything else works. Just 1 thing dont want to work.
I am using Jquery Mobile.
I cant stop the audio.
I am calling a function called "stopAudio()" that read a global variable "my_audio" and fire of the "stop()" function.
Here is my code.
Index.html
Stop Audio
AudioHandler.js
document.addEventListener("deviceready", onDeviceReady, false);
var my_media = null;
function playAudio(url) {
try {
var my_media = new Media(url,
// success callback
function () {
my_media.release();
},
// error callback
function (err) {
my_media.release();
});
// Play audio
my_media.play();
} catch (e) {
alert(e.message);
}
}
function stopAudio() {
my_media.stop();
}
controller.js
$("#stopaudio").click(function() {
stopAudio();
});
Example usage:
playAudio("/android_assets/www/mysound.mp3");
So i have these 3 files. What i posted is a small amount of the content, but the other content in those files have nothing to do with the audio. I did also try and comment everything out and so forth.
So, basicly the "controller.js" is the file where the click are detected, and from there the function "stopAudio()" are called that comes from AudioHandler.js.
My audio do start on startup of the phone.
I cant stop the audio.
Im running out of ideas, any help would be welcome :-)
Thanks :)
You've declared my_media as a local variable here:
try {
var my_media = new Media(url,
You want it to be global, so remove the var:
function playAudio(url) {
try {
my_media = new Media(url,
// success callback
function () {
my_media.release();
},
// error callback
function (err) {
my_media.release();
});
// Play audio
my_media.play();
} catch (e) {
alert(e.message);
}
}
I am trying to get the camera working from a button but am getting errors on the commented lines below. I am using the documentation provided by Phonegap/Cordova (v2.1). Any help appreciated.
var pictureSource = navigator.Camera.PictureSourceType.CAMERA; // Cannot read PictureSourceType of undef
var destinationType = navigator.camera.DestinationType.FILE_URI;
function onPhotoURISuccess(imageURI) {
var placeImage = document.getElementById('placeImage');
placeImage.src = imageURI;
placeImage.style.display = 'block';
console.log(imageURI);
}
function getPhoto() {
navigator.Camera.getPicture(onPhotoURISuccess, onFail, { //I am getting an error for this line saying camera is not defined?
quality: 50,
destinationType: camera.destinationType.FILE_URI,
sourceType: pictureSource
});
}
function onFail(message) {
alert('Failed because: ' + message);
}
Make sure you've got "deviceready" event first. For your call to getPicture() do this:
navigator.Camera.getPicture(onPhotoURISuccess, onFail, {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: pictureSource
});
I am also working on this. If you are talking about Android, you need to test on a real device. No matter you test on browser or simulator in Eclipse, it will give you Camera not defined error. I guess it is because hardware problem.