只是從快速瀏覽imgur和this 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.DEFAULT作爲第二選項Base64.encode。更新了上面的例子。
編輯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();
嘗試看imgur網站上的Java示例 - HTTP://api.imgur。 com/examples#uploading_java – hrickards
IMAGE.IO不存在於android中,那麼我該如何解決? – asd2005
假設您使用位圖存儲圖像,請參閱http://stackoverflow.com/questions/6344694/get-foreground-application-icon-convert-to-base64 – hrickards