2

I need help in using, imgur's API, to upload a photo and obviously retrieve a link.

IMGUR API: http://api.imgur.com/resources_anon

I'm able to get the URI for my image required to be uploaded but how can I implement the api above, I've downloaded mime4j and httpmime and added them to the libraries, but I can't seem to understand how to use them,

I looked at this but its confused me : Sending images using Http Post

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
asd2005
  • 315
  • 2
  • 5
  • 12

1 Answers1

3

Just from having a quick look at imgur and this question, I've come up with (pretty much just combined the two) the following. Let me know if it doesn't work.

Bitmap bitmap = yourBitmapHere;

// Creates Byte Array from picture
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); // Not sure whether this should be jpeg or png, try both and see which works best
URL url = new URL("http://api.imgur.com/2/upload");

//encodes picture with Base64 and inserts api key
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encode(baos.toByteArray(), Base64.DEFAULT).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(YOUR_API_KEY, "UTF-8");

// opens connection and sends data
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

Edit: It seems we need to pass Base64.DEFAULT as the second option to Base64.encode. Updated the example above.

Edit 2: Can you use the following code, based upon the oracle site, and report back what it outputs:

BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            conn.getInputStream()));

String inputLine;

while ((inputLine = ic.readLine()) != null) 
    System.out.println(inputLine);
in.close();
Community
  • 1
  • 1
hrickards
  • 1,061
  • 2
  • 9
  • 20
  • the part, "URLEncoder.encode(Base64.encode(baos.toByteArray()), "UTF-8");" comes up with a problem, Base64.encode(byte[],int) is not applicable for the arguement (byte[]) – asd2005 Aug 19 '11 at 13:45
  • Ok. I've updated the code above, but it seems the solution is to pass in Base64.DEFAULT as a second argument to Base64.encode. – hrickards Aug 19 '11 at 14:16
  • yeah i tried that straight away, but then it causes a problem with the "URLEncoder.encode" part of "URLEncoder.encode(Base64.encode(baos2.toByteArray()" Saying that encode requires String String, not Byte String :S – asd2005 Aug 19 '11 at 14:25
  • note, im using "baos2" as the name instead of baos, incase you think its an error – asd2005 Aug 19 '11 at 14:27
  • Try URLEncoder.encode(Base64.encode(baos2.toByteArry()).toString()... (Note the toString) – hrickards Aug 19 '11 at 14:37
  • Does it give you an error (shouldn't be the same thing about requiring String String if you have toString)? – hrickards Aug 19 '11 at 14:43
  • Did you mean : URLEncoder.encode(Base64.encode(baos2.toByteArray(), Base64.DEFAULT).toString(), "UTF-8"); – asd2005 Aug 19 '11 at 14:52
  • Yeah. That shouldn't be giving you errors saying it's Byte String, as I'm pretty sure toString will output a String :) – hrickards Aug 19 '11 at 14:53
  • yeah that did work, now last question my friend, how do i get hold of the link for that upload? – asd2005 Aug 19 '11 at 14:54
  • Is it okay to move this discussion to chat (chat link to the right of your username at the top of the page) and then report back with a working answer when we've found when? Edit: Ok, seems you don't have enough rep to talk on chat. No matter. See the update to my answer. – hrickards Aug 19 '11 at 15:03
  • by ic.readLine(), i think you ment in.readLine() ?? cause that outputs: java.io.outputStreamWriter@46c9c8bo – asd2005 Aug 19 '11 at 15:18
  • yeah the output is what i stated above – asd2005 Aug 19 '11 at 15:27
  • Hey check out the following post, ive tried a different way to implement it http://stackoverflow.com/questions/7124484/android-uploading-a-photo-to-host-on-imgur-programatically – asd2005 Aug 19 '11 at 16:18
  • Looks like you fixed it, well done :) You might want to add an answer here and on the other question with your solution, and mark them as accepted though. This will make it easy for others with the same problem to see your final solution. – hrickards Aug 20 '11 at 11:32