Reputation: 257
I am building a mobile app with Ionic/Cordova/Angular and I don't know how to get a image file by path (to be sent it to the server).
I'm using this example (http://devdactic.com/how-to-capture-and-store-images-with-ionic/ ) to get and save the image in app folder.
So I have an image name and an image path and I want to get somehow the image object to be able to convert it in base64 and send it to the server.
Any ideas ?
UPDATE:
`window.resolveLocalFileSystemURL($scope.user.image, onSuccess, onError);
function onSuccess(entry) {
/* Here: entry look like this :
"isFile":true, "isDirectory":false, "name":"myImgName1234", "fullPath": "/myImgName1234", "filesystem":"FileSystem:files>", "nativeURL":"file:///data/data/com.example.DemoApp/files/myImgName1234"
*/
$cordovaFile.readAsDataURL(cordova.file.dataDirectory, entry.name)
// I also tried : $cordovaFile.readAsDataURL(entry.nativeURL, entry.name) or $cordovaFile.readAsDataURL(entry.fullPath, entry.name)
.then(function (success) {
// success
window.alert('Succes read= '+ JSON.stringify(success));
}, function (error) {
// error
window.alert('Error read= '+ JSON.stringify(error));
});
}
function onError(error){
window.alert('error' + JSON.stringify(error));
}`
What I have also tried
Error code: 5 <- $cordovaFile.readAsDataURL(cordova.file.dataDirectory, entry.name)
Error code: 5 <- $cordovaFile.readAsDataURL(entry.nativeURL, entry.name)
Error code: 1000 <- $cordovaFile.readAsDataURL(entry.fullPath, entry.name)
Error code: 1 <- $cordovaFile.readAsDataURL(entry.name)
Is there any way to get the image Object (binary) if you know the path with cordova ? not just the image properties object (fileEntry)
Upvotes: 0
Views: 3466
Reputation: 3199
use this plugin to get the file from the device: http://ngcordova.com/docs/plugins/file/ and then do what ever you want with it.
have you used the ngCordova plugin? your code should look something like this:
module.controller('MyCtrl', function($scope, $cordovaFile) {
$cordovaFile.checkFile(cordova.file.dataDirectory, "file_name.txt")
.then(function (success) {
console.log(success);
}, function (error) {
console.log(error);
});
});
Upvotes: 2