0

我一直在阅读 imgur.com 的示例以及关于如何将图像上传到 imgur.com 的 stackoverflow.com

上传图片我使用这个代码:

 Bitmap bitmap = image;

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

此代码没有错误。但是当我添加以下代码时,应该检索图像的 url。我得到一个空指针异常。以下代码在 wr.flush() 下;

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

 String inputLine;

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

任何人都知道为什么我会得到这个 NPE?或者更好的方法来做到这一点?

 08-22 04:58:52.810: E/AndroidRuntime(26104): FATAL EXCEPTION: main
 08-22 04:58:52.810: E/AndroidRuntime(26104): java.lang.NullPointerException
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at se.xflash.myapp.ViewContact.uploadImgur(ViewContact.java:540)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at se.xflash.myapp.ViewContact.onOptionsItemSelected(ViewContact.java:340)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at android.app.Activity.onMenuItemSelected(Activity.java:2507)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:979)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at android.view.View$PerformClick.run(View.java:14329)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at android.os.Handler.handleCallback(Handler.java:605)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at android.os.Handler.dispatchMessage(Handler.java:92)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at android.os.Looper.loop(Looper.java:137)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at android.app.ActivityThread.main(ActivityThread.java:4511)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at java.lang.reflect.Method.invokeNative(Native Method)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at java.lang.reflect.Method.invoke(Method.java:511)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
 08-22 04:58:52.810: E/AndroidRuntime(26104):   at dalvik.system.NativeStart.main(Native Method)

这是完整的代码:

 public void uploadImgur() {

    String API_KEY = "***********************";
    String uploadpath = ((MyVariables) getApplication()).getUploadPath();

    Bitmap bitmap = BitmapFactory.decodeFile(uploadpath);

    // Creates Byte Array from picture
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);


    try {
        URL url = new URL("http://api.imgur.com/2/upload");
        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(API_KEY, "UTF-8");

        URLConnection   conn = url.openConnection();

        conn.setDoOutput(true);
        OutputStreamWriter  wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write(data);
        wr.flush();

        Log.e("wr.write", "Writing data: " + data);

    // Get the response

    Log.e("GET THE RESPONSE", "Debug 1");


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

    String inputLine;

    while ((inputLine = in.readLine()) != null)
            Log.e("Uploadpath is: ", inputLine);

        in.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("get response", "CRASH");
    }
}
4

1 回答 1

0

告诉我项目中是否需要包含库,以及如何生成 KEY api

于 2014-08-28T09:49:57.893 回答