7

Is it possible to store images in datastore? I want to cache some images for offline viewing.

  • what solution did you end up using? – Phil B Jun 11 '14 at 20:56
4

A better place to store images is the filesystem of the device. You can do this via the Cordova File functionality, documented at:

http://cordova.apache.org/docs/en/2.3.0/cordova_file_file.md.html

Raymond Camden wrote a couple of blog posts that demonstrate how to store images locally for Android and iOS.

Blog 1

Blog 2

  • Would this be considered secure storage? – Phil B Jun 11 '14 at 20:23
0

Yes we can securely store images in smart store. steps to implement:

1)Create soup:

"indexSpecs":[{"path":"Base64","type":"string"},{"path":"ParentId","type":"string"}]
 navigator.smartstore.registerSoup('ImageStore', indexSpecs, successCallback, errorCallback);

2)Configure Cordova Camera plugin:

Configure camera return option as DATA_URL to get result as base64.

navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
        destinationType: Camera.DestinationType.DATA_URL });
    function onSuccess(imageData){
        //Once we get the base64, simply store it in app's smart store.
        var temp = new Object();
        temp.Base64 = "data:image/jpeg;base64," + imageData;
        temp.ParentId = parentId; //parent record Id
        navigator.smartstore.upsertSoupEntries('ImageStore', [temp], function(data){
            if(data){
                console.log('image cached successfully');    
            }
        });
    }

3)Store base64 string into smart store and upload it to cloud if needed As we know salesforce mobilesdk's smart store encrpytes its data. Also for cloud sync, its easy to query base64 string and upload it to salesforce.

var querySpec = navigator.smartstore.buildExactQuerySpec("ParentId", parentId);
        navigator.smartstore.querySoup('ImageStore', querySpec, function(response){
            if(response.totalEntries>0){
                var data = response.currentPageOrderedEntries[0]; 
                createAttachment(data); 
            }
        },function(error){
            console.log(error);
        });  
        function createAttachment(data){
            force.create('Attachment',data).then(
                function (dataResponse) {
                    console.log('Attached successfully');
                }
            );
        }

In this approach third party apps not able to see our captured image. Let me know if you have any other questions.

Ref : Smartstore Documentation https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/offline_soup.htm

Camera plugin https://github.com/apache/cordova-plugin-camera

  • This is not really an answer. Can you please add examples, detail to it? Otherwise, it might get removed as a non-response. – Sebastian Kessel Jul 26 '16 at 15:06
  • 1
    @HariHaraSuthan Could you add more details to your answer please. This is a two year old question that you've just posted an answer to, so it appears out of context and you've not told us how you'd do what you describe. I'm confident that methods available 2yrs ago are quite different than what's available now. – crmprogdev Jul 26 '16 at 15:39
  • @HariHaraSuthan Please edit your question rather than adding it in comments. – Robin De Bondt Jul 27 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.