2011-08-18 2 views
2

메신저 안드로이드에 새로운 안드로이드에 대한 imgur를 통해 사진을 업로드, 프로그램

내가 사진을 업로드하고 분명 링크를 검색하려면, imgur의 API 사용에 도움이 필요합니다.

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

임은 새로운 저와의 그렇게 노출 된 API에.

업로드해야하는 이미지의 URI를 가져올 수 있지만 위의 API를 구현할 수있는 방법은 입니다. mime4j 및 httpmime을 다운로드하고 라이브러리에 추가했지만 사용 방법을 이해하는 것 같습니다. 그들은

나는이 바라 보았다하지만 나를 혼동 : 누군가가 나에게 예를 들어 줄 수 있다면 Sending images using Http Post

가 도움이 아주 많이. 고맙습니다.

+0

imgur 웹 사이트의 자바 예를보고 시도 - HTTP : //api.imgur. co.kr/examples # uploading_java – hrickards

+0

IMAGE.IO는 안드로이드에 존재하지 않으므로 어떻게 되나요? – asd2005

+0

비트 맵을 사용하여 이미지를 저장한다고 가정하면 http://stackoverflow.com/questions/6344694/get-foreground-application-icon-convert-to-base64 – hrickards

답변

3

imgurthis question을 간단히 살펴 보았습니다. 다음과 같이 작성했습니다. 작동하지 않는지 알려주세요.

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(); 

편집 : Base64.encode의 두 번째 옵션으로 Base64.DEFAULT를 전달해야합니다. 위의 예제를 업데이트했습니다.

편집 2 : 당신은 the oracle site에 따라 다음과 같은 코드를 사용하고 출력하는 것을 다시보고 수 :

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

String inputLine; 

while ((inputLine = ic.readLine()) != null) 
    System.out.println(inputLine); 
in.close(); 
+0

나는 그것을 테스트 할게요, – asd2005

+0

부품, "URLEncoder.encode (Base64.encode (baos.toByteArray())", "UTF-8"); " Base64.encode (byte [], int)는 arguement (byte [])에 적용 할 수 없다. – asd2005

+0

Ok. 위의 코드를 업데이트했지만 Base64.encode의 두 번째 인수로 Base64.DEFAULT를 전달하는 것이 해결책 인 것 같습니다. – hrickards