subodh
subodh

Reputation: 6158

How to upload an audio file in salesforce using android development?

I am using SalesforceMobileSDK-Android to develop an android application. I am able to develop a very basic android application, In my application, I am able to fetch the contact,account,lead etc details from the salesforce account and perform crud operation on these data. In my android application,I have a button called uploadFile, Now want to upload an audio file on click of that button, I am not able to find any rest api which will help me to upload it on Salesforce from my android client application.

If there is any sample url or source code or any helpful resource please provide me.

Thanks

Upvotes: 0

Views: 1062

Answers (2)

eyescream
eyescream

Reputation: 19612

You'll have to experiment with base64 encoding the file and sending a POSTrequest to /services/data/v26.0/sobjects/attachment/{parent record id}/body endpoint. I haven't done it myself but there are some nice examples:

  1. http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert_update_blob.htm - uses different approach with json message.
  2. http://blogs.developerforce.com/developer-relations/2011/09/using-binary-data-with-rest.html - if you can create a server-side REST service.
  3. Check resources on Salesforce-dedicated Stack site, for example https://salesforce.stackexchange.com/questions/1301/image-upload-to-chatter-post
  4. Last but not least - check the Salesforce community boards, for example http://boards.developerforce.com/t5/APIs-and-Integration/inserting-an-attachment-via-REST/td-p/322699

Upvotes: 1

Marius M
Marius M

Reputation: 496

It's mostly server side you have to be concerned about when uploading files, client side you could have an approach like this (just get the idea, it's not a full functional code):

 FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
 // open a URL connection to the Servlet
 URL url = new URL(urlString);
 // Open a HTTP connection to the URL
 conn = (HttpURLConnection) url.openConnection();
 // Allow Inputs
 conn.setDoInput(true);
 // Allow Outputs
 conn.setDoOutput(true);
 // Don't use a cached copy.
 conn.setUseCaches(false);
 // Use a post method.
 conn.setRequestMethod("POST");
 conn.setRequestProperty("Connection", "Keep-Alive");
 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
 dos = new DataOutputStream( conn.getOutputStream() );
 dos.writeBytes(twoHyphens + boundary + lineEnd);
 dos.writeBytes("Content-Disposition: form-data; name:\"uploadedfile\";filename=\"" + selectedPath + "\"" + lineEnd);
 dos.writeBytes(lineEnd);
 // create a buffer of maximum size
 bytesAvailable = fileInputStream.available();
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 buffer = new byte[bufferSize];
 // read file and write it into form...
 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
 while (bytesRead > 0)
 {
  dos.write(buffer, 0, bufferSize);
  bytesAvailable = fileInputStream.available();
  bufferSize = Math.min(bytesAvailable, maxBufferSize);
  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
 }
 // send multipart form data necesssary after file data...
 dos.writeBytes(lineEnd);
 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
 // close streams
 Log.e("Debug","File is written");
 fileInputStream.close();
 dos.flush();
 dos.close();

Upvotes: 0

Related Questions