I am trying to upload an image from Android based phonegap application to Google drive. I use the following code to get the image URI and use phonegap file transfer plugin to upload to Google. I am looking for how do we get the file name using the phonegap APIs. The filename and path that I get using the code doesn't give me the real file path and name which should be in "image.jpg" format, rather it gives in following format - content://media/external/images/media/47 I have searched and found that there are ways to convert URI to absolute path (Get filename and path from URI from mediastore) but all are native code. I am using phonegap framework.
uploadFile: function() {
navigator.camera.getPicture(
uploadPhoto,
function(message) { console.log("Failed to get pic"+message); },
{
quality : 50,
destinationType : navigator.camera.DestinationType.NATIVE_URI,
sourceType : navigator.camera.PictureSourceType.PHOTOLIBRARY
}
);
function uploadPhoto(imageURI) {
console.log("File read"+imageURI);
var options = new FileUploadOptions();
//options.fileKey="file.png";
//options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
//options.fileName="googfile.png";
//options.mimeType="image/png";
var params = {};
params.uploadType = "media";
options.params = params;
var headers={'Content-Type': 'image/png', 'Authorization': 'Bearer '+localStorage.access_token};
options.headers = headers;
var ft = new FileTransfer();
//console.log("File name ="+options.fileName);
//console.log("File transfer URI ="+imageURI);
ft.upload(imageURI, encodeURI("https://www.googleapis.com/upload/drive/v2/files"), win, fail, options);
}
function win(resp) {
console.log("Code = " + resp.responseCode);
console.log("Response = " + resp.response);
console.log("Sent = " + resp.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
}
};