flickr upload in Android

All your problems with Audio, Video and Images.

flickr upload in Android

Postby JoyLakh » Tue Dec 15, 2009 5:33 pm

Hello All,

Below pasted code helps you in uploading an image to flickr.
Dependencies are 'aPhotoPath' is the file path on device, 'apiKeyString' is a string holding your apiKey, 'tokenString' holds the token returned by flickr.auth.getFullToken call and 'getMD5(String)' is a method that returns MD5 signature.

public void UploadToFlickr(String aPhotoPath)
{
String lBoundary = "--"+Math.random();

HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;

try{
FileInputStream lFileInputStream = new FileInputStream(aPhotoPath);

URL url = new URL("http://api.flickr.com/services/upload/");

conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + lBoundary);
conn.connect();

dos = new DataOutputStream( conn.getOutputStream() );

dos.writeBytes("\r\n" + "--" + lBoundary + "\r\n");
dos.writeBytes("Content-Disposition: form-data; name=\"api_key\"\r\n\r\n");
dos.write(apiKeyString.getBytes("UTF-8"));
dos.writeBytes("\r\n" + "--" + lBoundary + "\r\n");

dos.writeBytes("Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n");
dos.write(tokenString.getBytes("UTF-8"));
dos.writeBytes("\r\n" + "--" + lBoundary + "\r\n");

dos.writeBytes("Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n");
dos.write(getMD5(shareSecretKey+"api_key"+apiKeyString+"auth_token"+tokenString).getBytes("UTF-8"));
dos.writeBytes("\r\n" + "--" + lBoundary + "\r\n");

dos.writeBytes("Content-Disposition: form-data; name=\"photo\"; filename=\"image.jpg\";\r\n");
dos.writeBytes("Content-Type: image/jpeg" + "\r\n\r\n");

byte[] buf = new byte[512];
int res = -1;
while ((res = lFileInputStream.read(buf)) != -1) {
dos.write(buf);
}

dos.writeBytes("\r\n" + "--" + lBoundary + "\r\n");

lFileInputStream.close();
dos.flush();
dos.close();

}catch(Exception aException){
String lExcepString = aException.getMessage();
lExcepString = "";
}

try
{
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
str = str.concat("");
str = "";
}
inStream.close();
}
catch (IOException ioex)
{

}
}

I spent much time on this. I know that we can do much better coding in Android. I hope you save your time in re-inventing this and spend it in improvising this.
Celebrate Life.
JoyLakh
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Dec 16, 2008 5:42 am
Location: Bangalore, India

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 20 guests