0

I have been reading on imgur.coms examples and also on stackoverflow.com on how to upload images to imgur.com

to upload the image i use this code:

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

this code gives no errors. but when im adding this following code that is supposed to retrieve the url to the image. i get a nullpointer exception. The following code is under wr.flush();

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

 String inputLine;

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

Anyone has any idea why i get this NPE? or a better way to do this?

 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)

this is the full code:

 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");
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rudman
  • 11
  • 4
  • First, please post your LogCat so we can see what line it is failing on. Second, instead of `new BufferedReader(new InputStreamReader(conn.getInputStream()));` try `new BufferedReader(conn.getInputStream());` and see if that makes a difference. Third, use `Log` for output rather than `System.out`, eg `Log.d("MyApp",inputLine);`. – Ken Y-N Aug 22 '12 at 02:38
  • Hi. i have updated the post with the error log. The NPE comes from while ((inputLine = in.readLine()) != null). ah, yes i normaly use Log.e. I tried to change to new BufferedReader(conn.getInputStream()); but that gives me the error "The bufferedReader(inputStream) is undefined and i dont know how to do that hehe. thank you for trying to help me :) – Rudman Aug 22 '12 at 03:06
  • If i change the URL to http://www.oracle.com, its working. i retreive the webpage. but when i have url http://api.imgur.com/2/upload i get NPE. so weird – Rudman Aug 24 '12 at 07:44

1 Answers1

0

Tell me is there library need to include in project , and how to generate KEy api

user3710617
  • 29
  • 1
  • 7