上传于Android编程通过imgur照片

问题描述 投票:2回答:1

我需要使用,imgur的API,上传照片,显然检索链接帮助。

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

我能够得到的URI需要上传自己的形象,但我怎么能实现上述API,我已经下载mime4j和httpmime,并将它们添加到库,但我似乎无法理解如何使用它们,

我看着这一点,但它让我感到困惑:Sending images using Http Post

java android upload photo imgur
1个回答
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.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();
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.