Search code examples
androidfacebook-android-sdkprofile

Error while getting facebook profile picture in Android


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

Solution

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