Maroš Šeleng
Maroš Šeleng

Reputation: 1640

javascript - Get filename and extension from FILE_URI in Ionic

I need some hint on how to get the name of a file, that has been picked/photographed by the cordova camera plugin. Here is the code, I have for getting a picture:

var options = {destinationType: Camera.DestinationType.NATIVE_URI};
options.sourceType = sourceType;
$cordovaCamera.getPicture(options).then(function (newURI) {
  imageFileURI = newURI;
  console.log('NATIVE_URI:\n' + imageFileURI);
  //I need to get the file name and extension here
  window.plugins.Base64.encodeFile(imageFileURI, function (base64Image) {
    base64Data = base64Image;  
  }, function (error) {
    console.log('Error while converting to base64: ' + error);
  });
}, function (error) {
 console.log('Error while picking a photo: ' + error);
});

Is there a way, how can I get the file name and the extension, where I need it? Thank you

Upvotes: 2

Views: 9275

Answers (3)

SRIPAD.B.V
SRIPAD.B.V

Reputation: 53

var fileName = imageFileURI.substr(imageFileURI.lastIndexOf('/') + 1);

The above code to get the file name.

var fileExtension = filename.substr(filename.lastIndexOf('/') + 1);

this will get the file extension.

Upvotes: 5

Nimeshka Srimal
Nimeshka Srimal

Reputation: 8920

Not sure if it's too late to answer this question.

This is how I solved the exact issue. Problem is it gives you the file URI when the image is in phone memory, and a content:// url when it's from the external storage.

Solution is to use this plugin. cordova-plugin-filepath.

You can read the above github page for instructions. This is how I used it.

  window.FilePath.resolveNativePath(
    ImageURI, 
    function(imagePath){
      $(el).html(imagePath);
    }, 
    function(){
      alert('Could not retrieve the image path'); 
    });

Hope this will be helpful.

Upvotes: 1

TheBosZ
TheBosZ

Reputation: 657

A couple of thoughts:

  • As beaver says, try FILE_URI
  • It looks like you're trying to get an image in Base64, correct? If so, you can use the ngCordova file plugin and use readAsDataURL()
  • If neither of these work, you can use resolveLocalFileSystemURL

Upvotes: 0

Related Questions