Search code examples
androidtitaniumandroid-cameraappceleratorappcelerator-mobile

Titanium 3.X getFile() from local storage


Using Titanium on Android 4+ I want to access a jpeg file which has been taken with the camera. I need to achieve 2 objectives, namely, return the EXIF data and transfer the bytes to an API endpoint. My problem is I'm unable to access the file...

I'm using a 3rd party module to handle the file selection (Multi Image Picker) which returns a list of file locations, using the File Manager app on the emulator (GenyMotion) I can confirm the location on disk is correct. However, the following always returns false...

var file = Ti.Filesystem.getFile('/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg');
Ti.API.info('Do we have a file? ' (file.exists()? 'YES' : 'NO'));

The output for the above would be... Do we have a file? NO

Further reading shows Titanium has 5 predefined folder locations which can be passed into the getFile() method and one possible reason for the above code not working would be it is defaulting to the 'Resouces' folder location? That said all but one folder location is app specific, the exception being externalStorageLocation. Now my understanding of an Android device is that any image taken with the camera will be stored on the internal storage system unless an SD card is present. This is true in my case as the following lists 0 files...

var extDir = Ti.Filesystem.getExternalStorageDirectory();
var dir = Ti.Filesystem.getFile(extDir);
var dir_files = dir.getDirectoryListing();

Ti.API.info('External files... ' + dir_files.length);

The output for the above would be... External files... 0

So am I right in thinking Appcelerator have simply not included the ability to access local storage (outside of any app specific folders) within their API? Or am I missing something and there is in fact another way?


Solution

  • Thanks to @Bharal I was able to find a solution...

    By using the Ti.Media.openPhotoGallery() method I was able to identify the correct native path for the image by inspecting the event object returned from the success callback.

    The path was missing 'file://' at the beginning, I couldn't be 100% sure but I suspect this forces the getFile() method to use an absolute path and not a relative path from within the Resources folder.

    To confirm, the following will return a file object...

    var file = Ti.Filesystem.getFile('file://[path]');
    

    Where [path] is the folder location as reported within the File Manager app on the device, for example '/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg'