I'm trying to copy a file from photo gallery to dataDirectory on Android 5.0 device. Unfortunely Cordova camera plugin returns path in format like: content://media/external/images/media/8793
var options = {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
saveToPhotoAlbum: false
};
return $cordovaCamera.getPicture(options).then(function (result) {
return handlePictureResult(result);
})
$cordovaFile.copyFile('content://media/external/images/media/8793', null, cordova.file.dataDirectory, newFileName) simply doesn't work.
Any ideas how can I copy file from gallery (path with content://) to cordova.file.dataDirectory?
Here is how you should handle it (with fileTransfer plugin instead of file one) :
app.controller(
'MainController',
[
'$scope', '$cordovaCamera', '$cordovaFileTransfer',
function($scope, $cordovaCamera, $cordovaFileTransfer)
{
$scope.images = { imageUri: '' };
$scope.getImage = function()
{
var options = {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(
function(fileUri)
{
console.log(fileUri);
$cordovaFileTransfer.download(fileUri, cordova.file.dataDirectory + 'my-image.jpg', {}, true).then(
function(fileEntry)
{
$scope.images.imageUri = fileEntry.nativeURL;
},
function (error)
{
console.log(error);
}
);
},
function(error)
{
console.log(error);
}
);
}
}
]
);
And the HTML (just in case) :
<section ng-controller="MainController">
<img ng-src="{{ images.imageUri }}">
<button ng-click="getImage()">GET IMAGE</button>
</section>
You can received the image data on base64 and save it yourself.
I did a library for easy manage of FileAPI on cordova/HTML5, maybe is usefull for you: https://github.com/exos/cofs
var fs = new CoFS();
var options = {
quality: 50,
destinationType : navigator.camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY
};
return $cordovaCamera.getPicture(options).then(function (data) {
fs.writeFile(
cordova.file.dataDirectory + '/example.jpg',
data,
'base64',
function (err) {
// if not err, success!
}
);
});