function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhiraj Sharma 10Dhiraj Sharma 10 

Upload Photo In Opportunity of salesforce.com through android app

I am making an android application which use salesforce.com API. Maximum task i am able to do through sdk but I have problem when it comes to adding custom field in Opportunity object for photo.I researched and find out this link https://success.salesforce.com/answers?id=90630000000glUkAAIThrough this I made a custom field of Rich Text Area in Opportunity and i am successfully able to upload image. But the problem is how to accomplish the same through android code??? I mean a new field is added into API of Opportunity list as seen below in the detail of field.

Field name: image__c Field Label: image Type is: textarea Length: 131072 This is a custom field. Can be nulled. Createable Updateable

But since its data type is textarea, How to send the image captured from phone camera or selected from gallery while creating new Opportunity.
Gaurav KheterpalGaurav Kheterpal
Can you confirm if you're building this natively or hybrid?

If it's hybrid, you can use the Cordova navigator.camera.getPicture() call as follows
 
function getPhotoAndUploadToContact(name, contactId) {
    SFHybridApp.logToConsole("in capturePhoto, contactId = "+contactId);

    $j('#Image').attr('data-old-src', $j('#Image').attr('src'));
    $j('#Image').attr('src', "images/camera.png");

    navigator.camera.getPicture(function(imageData){
        onPhotoDataSuccess(imageData, name, contactId);
    }, function(errorMsg){
        // Most likely error is user cancelling out of camera
        $j('#dialog-text').html(errorMsg);
        $j.mobile.changePage('#jqm-dialog');
        $j('#Image').attr('src', $j('#Image').attr('data-old-src'));
        $j('#Image').removeAttr('data-old-src');
    }, {
        quality: 50, 
        sourceType: Camera.PictureSourceType.CAMERA,
        destinationType: Camera.DestinationType.DATA_URL
    });
}
You can then upload it using the following approach
 
function onPhotoDataSuccess(imageData, name, oppid) {
    var $j = jQuery.noConflict();

    SFHybridApp.logToConsole("in onPhotoDataSuccess, oppid = "+oppid);

    // Update the image on screen
    $j('#Image').attr('src', "data:image/jpeg;base64," + imageData);

    // Upload the image data to Content
    $j.mobile.showPageLoadingMsg();
    forcetkClient.create('ContentVersion', {
        "PathOnClient" : name + ".png",
        "VersionData" : imageData
    }, function(data){
        // Now update the Contact record with the new ContentVersion Id
        SFHybridApp.logToConsole('Created ContentVersion ' + data.id);
        forcetkClient.update('Opportunity', opportunityid, { 
            "Image_ID__c" : data.id 
        }, function(){
            $j.mobile.hidePageLoadingMsg();
            SFHybridApp.logToConsole('Updated Opp '+oppid);
        }, onErrorSfdc);
    }, onErrorSfdc);    
}
Hope this helps.

If my answer helps resolve your query, please mark it as the 'Best Answer' to benefit others and improve the overall quality of Discussion Forums.

Gaurav Kheterpal
Certified Force.com Developer| Developer Forums Moderator| Dreamforce Speaker


    



 
Dhiraj Sharma 10Dhiraj Sharma 10
Sorry Gaurav Sir I forgot to mention. I am building in android native. Thanks for the code you provided. But i would appreciate if you could provide the same in android native