saving image into gallery with phonegap - android

How can I save an image to gallery of the mobile with android phonegap?
I tried this code:
function save(){
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("pic.jpg", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
console.log(fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
var photo = document.getElementById("dwnldImg");
writer.write(photo.value);
}
But it saves the image into my project directory which is file:///data/data/pack.name/pic.jpg.
how can i make it appear in the mobile gallery

try using this:
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail,{
quality : 25,
destinationType : Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : true,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: true });
}

Related

Store Image into internal Storage in Ionic?

i am new in Ionic, i am trying to get picture and want to store copy or store image into internal storage so i can get back captured image on any view.
I don't know how to store images in internal Storage.
I have read this.
this is Helpful but not solving my issue.
My Controller is here.
Help me in this.
Controller
/**** tab Camera Controller ***/
.controller('tabCameraCtrl', function($scope, $state, $cordovaCamera) {
/**********function to Open Camera *********/
$scope.openCamera = function() {
document.addEventListener("deviceready", function() {
var options = {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
// allowEdit: true,
//encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
//popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
// correctOrientation: true
};
$cordovaCamera.getPicture(options).then(function(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
$scope.lastPhoto = imageURI;
window.localStorage.setItem('image',JSON.stringify(imageURI));
console.log('success')
console.log('Image Address');
console.log(imageURI);
}, function(err) {
// error
console.log(err)
});
}, false);
}
Help me to solve this problem.

how to select the video from gallery in phonegap?

Please help me any one how to select only the video from the gallery in android and iOS in cordova
I am tried this click Here for select the video from media but it not working for me...
var pictureSource;
var destinationType;
var mediaType;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
mediaType = navigator.camera.MediaType;
}
navigator.camera.getPicture(onPhotoURISuccess, onFail, {
destinationType: destinationType.FILE_URI,
mediaType: mediaType.VIDEO,
sourceType: source
});
function onPhotoURISuccess(imageURI) {
console.log(imageURI);
}
function onFail(message) {
console.log(message);
}
i am used this same code to implement in my app but it can't come....
You can try the snippet below:
navigator.camera.getPicture(onSuccess, onFail, { quality: 100,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
mediaType: Camera.MediaType.VIDEO
});
Not that the mediaType can be:
Camera.MediaType = {
PICTURE: 0, // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
VIDEO: 1, // allow selection of video only, WILL ALWAYS RETURN FILE_URI
ALLMEDIA : 2 // allow selection from all media types
For reference read this.

Phonegap - android - image saved in cache if orientation set to true

On setting correctOrientation : true, then image rotation issue is solved and thumbnail is set - But image path is: file://storage/emulated/0/Android/data/com.examole.helloworld/modified.jpg?149028394994
Without correctOrientation: true, image path is: file:///storage/emulated/0/DCIM/Camera/1490345556009.jpg
When trying to set another image with correctOrientation: true, then the latest selected image is not set.
Following is the code for your kind reference:
navigator.camera.getPicture(captureSuccess, onFail, {quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
mediaType: Camera.MediaType.PICTURE,
correctOrientation: true,
allowEdit: true
});
Thanks in Advance.
You can try this,
$(document).on('click','.capture_photo',function(){
navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
quality : 75,
destinationType : Camera.DestinationType.DATA_URL,
sourceType : Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.PNG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
});
});
// to call the success function of capture image(onPhotoDataSuccess)
function onPhotoDataSuccess(imageData) {
sessionStorage.setItem("img_api",imageData);
$('#captureimg').attr('src','data:image/jpeg;base64,' + imageData);
App.show_toast("Profile image updated successfully!");
}
//onfail
onFail(message) { alert('Failed because: ' + message); }
I have solved this and posted my answer
navigator.camera.getPicture(captureSuccess, onFail, {quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
mediaType: Camera.MediaType.PICTURE,
correctOrientation: true,
});
var captureSuccess = function(mediaFiles) {
var fD = mediaFiles;
window.resolveLocalFileSystemURI(fD, function(fileEntry) {
fileEntry.file(function(fT) {
var fname = fileEntry.nativeURL.substr(fileEntry.nativeURL.lastIndexOf('/') + 1);
fileTransferUpload(fD,fname);
}, function() {
console.log('error');
});
}, onFError);
};
the method fileTransferUpload above will just set the image path in src.
in success callback function, image path is received and without getting nativeURL from this path, i have set the path itself in src.
Here's a better solution (involved a spot of digging through some Java files, but takes about 8 seconds)
in CameraLauncher.java (in the cordova-plugin-camera/src/android folder)
//Old:
String modifiedPath = getTempDirectoryPath() + "/modified"+".jpg";
this.callbackContext.success("file://" + modifiedPath + "?" + System.currentTimeMillis());
AKA just move the timestamp up in the filename.
//New:
String modifiedPath = getTempDirectoryPath() + "/modified"+ System.currentTimeMillis() +".jpg";
this.callbackContext.success("file://" + modifiedPath);
document.addEventListener("deviceready",onDeviceReady,false);
var pictureSource; // picture source
var destinationType; // sets the format of returned value
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
sorry, you want to add this code along with the that.

Cordova Phonegap File plugin FileWriter cordova.file

Why this works :
myFile1 = "myReadme.txt";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile(myFile1, {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
alert("good");
}
function fail(error) {
alert("Error");
}
and that doesn't work ?
myFile2 = cordova.file.externalDataDirectory + "myReadme.txt";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile(myFile2, {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
alert("good");
}
function fail(error) {
alert("Error");
}
Can we not use cordova.file.dataDirectory or cordova.file.externalRootDirectory or externalDataDirectory for Android ?
Thank you
I'm gonna try to answer with this issue (but I'm sure we can to do better ? )
First, when I plug my device to my computer the tree path is about like that :
MyFile1.txt
MyApplication
MyFiles
MyCache
Application
DCIM
media
Movies
Musics
Android
data
data (I think is not accessible when your are not rooted ???)
com.MyDomainName.MyAppName
cache
files
Then,
Following this official Cordova document : http://plugins.cordova.io/#/package/org.apache.cordova.file
, My first wish is to store my files and datas with "cordova.file.dataDirectory" or "cordova.file.externalDataDirectory" or another else ... ?
when you're doing console.log(cordova.file.dataDirectory) you obtain file:///data/data/com.MyDomainName.MyAppName/files/ => This, you cannot access when you are no rooted
and when you're doing console.log(cordova.file.externalDataDirectory) you obtain
file:///storage/emulated/0/
and when you're doing that
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) { console.log(fileSystem.root) ...
you can see in this object : nativeURL: "file:///storage/emulated/0/" ... so, it's the same like "externalDataDirectory" and I suppose we cannot use "dataDirectory" ...
Anyway, I don't know whether it's the best practice but here you are my solution :
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin, getDirectoryFail);
function onFSWin(fileSystem) {
fileSystem.root.getDirectory("myFolder", {create: true, exclusive: false}, getDirectorySuccess, getDirectoryFail);
}
function getDirectorySuccess(parent) {
console.log("New Folder or Folder already exists: " + parent.fullPath);
}
function getDirectoryFail(error) {
console.log("Unable to create new directory: " + error.code);
}
// Second, we gonna check or create a new file in a specific folder
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin2, onFSFail2);
function onFSWin2(fileSystem) {
fileSystem.root.getFile("myFolder/myFile1.txt", {create: true, exclusive: false}, gotFileEntry, onFSFail2);
}
function gotFileEntry(fileEntry) {
console.log("New file: " + fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, onFSFail2);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");
writer.truncate(11);
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample'");
writer.seek(4);
writer.write(" different text");
writer.onwriteend = function(evt){
console.log("contents of file now 'some different text'");
}
};
};
writer.write("some sample text");
}
function onFSFail2(error) {
console.log(error.code);
}
You can use resolveLocalFileSystemURL to use the cordova.file.* properties:
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dirEntry) {
dirEntry.getFile("myFile1.txt", {
create: true,
exclusive: false
}, function (fileEntry) {
// fileEntry.createWriter
}, onError);
});

Choose an image from camera or gallery using phonegap

<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;
}
function capturePhoto() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 25, destinationType:
Camera.DestinationType.FILE_URI });
}
function onPhotoURISuccess(imageURI) {
createFileEntry(imageURI);
}
function createFileEntry(imageURI) {
window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);
}
function copyPhoto(fileEntry) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
fileSys.root.getDirectory("photos", {create: true, exclusive: false},
function(dir) {
fileEntry.copyTo(dir, "file.jpg", onCopySuccess, fail);
}, fail);
}, fail);
}
function onCopySuccess(entry) {
console.log(entry.fullPath)
}
function fail(error) {
console.log(error.code);
}
</script>
You should use the PhoneGap 2.0.0 camera object. The documentation provides a full photo capturing example.
Furthermore the navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] ); takes a photo using the camera or retrieves a photo from the device's album. The image is returned as a base64 encoded String or as the URI of an image file.
I hope this helps.

Categories

Resources