上传Android上通过imgur照片编程
问题描述:
即时通讯新到Android,上传Android上通过imgur照片编程
我需要使用,imgur的API,上传照片,显然检索链接帮助。
IMGUR API: http://api.imgur.com/resources_anon
进出口新的API与我这么光秃秃的。
我能够获得需要上传的图片的URI,但是我如何实现上面的api, 我下载了mime4j和httpmime并将它们添加到库中,但我似乎无法理解如何使用其中,
我看着这一点,但它让我感到困惑: Sending images using Http Post
如果有人可以给我一个例子,这将有助于非常多。 谢谢
答
只是从快速浏览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