0

I am tryging to get the facebook profile image with the code below. I try the link that is in the log it seems fine, but in the getContent() part I get the error. Any ideas about why this is happening?

My code is:

 imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
                                            System.out.println("problem 7");
                                            System.out.println(imgUrl);
                                            InputStream in = (InputStream) imgUrl.getContent();
                                            System.out.println("problem 7.5");
                                            Bitmap  bitmap = BitmapFactory.decodeStream(in);
                                            System.out.println("problem 8");

The error is as below:

04-21 15:59:58.472 18711-18711/com.my_last.mylast I/System.out: problem 7
    04-21 15:59:58.472 18711-18711/com.my_last.mylast I/System.out: https://graph.facebook.com/135258820209927/picture?type=large
    04-21 15:59:58.481 18711-18711/com.my_last.mylast D/AndroidRuntime: Shutting down VM
    04-21 15:59:58.481 18711-18711/com.my_last.mylast W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb309c180)
    04-21 15:59:58.481 18711-18711/com.my_last.mylast E/AndroidRuntime: FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
    at java.net.InetAddress.getAllByName(InetAddress.java:220)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
    at java.net.URLConnection.getContent(URLConnection.java:194)
    at libcore.net.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:174)
    at java.net.URL.getContent(URL.java:447)
    at com.my_last.mylast.signIn$3$2.onCompleted(signIn.java:365)
    at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
    at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    04-21 15:59:58.992 18711-18713/com.my_last.mylast I/dalvikvm: threadid=3: reacting to signal 3

Edit:

I updated my code as below, and now getting error as below:

Thread thread = new Thread() {
                                            public void run() {
                                                Message message = new Message();
                                                try {
                                                    imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
                                                    System.out.println("problem 7");
                                                    System.out.println(imgUrl);
                                                    Bitmap bitmap = BitmapFactory.decodeStream(imgUrl.openConnection().getInputStream());
                                                    System.out.println("problem 8");
                                                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                                                    byteArray1 = stream.toByteArray();
                                                    image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");
                                                    image2 = new ParseFile("profilePhoto2.jpg", byteArray1, "image/jpg");
                                                } catch (Exception e) {
                                                    Log.e("MYAPP", "unexpected JSON exception", e);
                                                }


                                            }
                                        };
                                        thread.start();

Error message:

04-21 16:40:29.118 10885-12039/com.my_last.mylast I/System.out: problem 7
04-21 16:40:29.118 10885-12039/com.my_last.mylast I/System.out: https://graph.facebook.com/135258820209927/picture?type=large
04-21 16:40:29.118 10885-10885/com.my_last.mylast D/AndroidRuntime: Shutting down VM
04-21 16:40:29.118 10885-10885/com.my_last.mylast W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb309c180)
04-21 16:40:29.118 10885-10885/com.my_last.mylast E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    java.lang.IllegalArgumentException: value may not be null.
                                                                        at com.parse.ParseObject.performPut(ParseObject.java:2860)
                                                                        at com.parse.ParseObject.put(ParseObject.java:2851)
                                                                        at com.my_last.mylast.signIn$3$2.onCompleted(signIn.java:406)
                                                                        at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
                                                                        at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
                                                                        at android.os.Handler.handleCallback(Handler.java:605)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                        at android.os.Looper.loop(Looper.java:137)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:511)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                        at dalvik.system.NativeStart.main(Native Method)
04-21 16:40:29.648 10885-10892/com.my_last.mylast I/dalvikvm: threadid=3: reacting to signal 3
04-21 16:40:29.658 10885-10892/com.my_last.mylast I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
04-21 16:40:29.777 10885-12039/com.my_last.mylast I/System.out: problem 8
saner
  • 821
  • 2
  • 10
  • 32

1 Answers1

1

You can use Thread or AsyncTask to download your image in background thread. Here is an example of doing it with thread.

Thread  thread = new Thread() {
      public void run() {

        try{
         imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
                                            System.out.println("problem 7");
                                            System.out.println(imgUrl);
                                            InputStream in = (InputStream) imgUrl.getContent();
                                            System.out.println("problem 7.5");
                                            Bitmap  bitmap = BitmapFactory.decodeStream(in);
                                            System.out.println("problem 8");
        } catch (Exception e) {

        }


      }
    };
    thread.start();
  }
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • Hello Vivek now I am getting the error: java.lang.IllegalArgumentException: value may not be null. – saner Apr 21 '16 at 13:32
  • for which value is that exception for? – Vivek Mishra Apr 21 '16 at 13:34
  • for imgUrl.getContent() but the println(imgUrl) gives me the link in which after clicking the link I see the profile picture. – saner Apr 21 '16 at 13:35
  • post this stack trace in your question – Vivek Mishra Apr 21 '16 at 13:37
  • When you already have bitmap in run method then why you are again converting stream to image? See this line `Bitmap bitmap = BitmapFactory.decodeStream(imgUrl.openConnection().getInputStream());` – Vivek Mishra Apr 21 '16 at 13:53
  • I try to get the image and save it to my parse server. How should I update? – saner Apr 21 '16 at 13:58
  • Ok I put the upcoming part of my code into the thread part (where I save the photo into my database) and it startet to work. I guess it was because the image tried to be saved before it was decoded in the thread. Now it is working. Thank you very much Vivek. – saner Apr 21 '16 at 14:18