简体   繁体   中英

getting exceptions when loading facebook profile picture in android with facebook sdk 4+

im trying to load a facebook profile picture in my android app on thee top portion of a navigation drawer, but seem to be getting an exception. I have poured through stack overflows explaining how to load the image using bitmap, picasso, etc, but have been unable to load it for some reason still. I believe the issue is some network thread issue, but i thought that because i execute the method with .executeAsync() that it would not matter. Here is the xml where i declare the image that is to be a profile picture (nav_header_main.xml):

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:src="@drawable/hanger_circle"
    app:border_color="#FF000000"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"/>



</RelativeLayout>

Here is the snippet from my activity where i request the picture (on success of facebook login:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //initialize facebook sdk
    FacebookSdk.sdkInitialize(getApplicationContext());

    setContentView(R.layout.activity_login);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);

    //call custom application class to initialize intercom (not login, just setup)
    ApplicationHelper helper = (ApplicationHelper)getApplicationContext();

    //create facebook button recognition
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    //facebook picture stuff
                    Bundle params = new Bundle();
                    params.putString("fields", "id,email,gender,cover,picture.type(large)");
                    new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET,
                            new GraphRequest.Callback() {
                                @Override
                                public void onCompleted(GraphResponse response) {
                                    if (response != null) {
                                        try {
                                            JSONObject data = response.getJSONObject();
                                            if (data.has("picture")) {
                                                String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                                                // set profile image to imageview using Picasso or Native methods
                                                Bitmap profilePic = getFacebookProfilePicture(profilePicUrl);
                                                ImageView mImageView = (ImageView) findViewById(R.id.profile_image);
                                                mImageView.setImageBitmap(profilePic);
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }).executeAsync();



                    // launch next activity
                    Toast.makeText(getApplicationContext(), "SUCCESS", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();

                }

                @Override
                public void onCancel() {
                    // App code
                    Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                    Toast.makeText(getApplicationContext(), "FAIL", Toast.LENGTH_SHORT).show();

                }
            });


    //check if facebook user is logged in already
    AccessToken token = AccessToken.getCurrentAccessToken();
    Profile prof = Profile.getCurrentProfile();
    if(token != null && prof != null){
        //log in
        //Toast.makeText(getApplicationContext(), token.getToken(), Toast.LENGTH_SHORT).show();
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }
    //check if regular user is logged in already
    SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
    if(shared.contains("username") && shared.contains("password")){
        //password exists in file so launch next activity (make sure there correct with API)
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }


}

I also use this helper method:

public static Bitmap getFacebookProfilePicture(String url){
    URL facebookProfileURL= null;
    Bitmap bitmap = null;
    try {
        facebookProfileURL = new URL(url);
        bitmap = BitmapFactory.decodeStream(facebookProfileURL.openConnection().getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

Here is my exception: 05-26 14:20:05.751 11101-11101/com.android.press.press W/System.err: android.os.NetworkOnMainThreadException 05-26 14:20:05.751 11101-11101/com.android.press.press W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 05-26 14:20:05.751 11101-11101/com.android.press.press W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 05-26 14:20:05.751 11101-11101/com.android.press.press W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 05-26 14:20:05.751 11101-11101/com.android.press.press W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215) 05-26 14:20:05.751 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232) 05-26 14:20:05.7 52 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190) 05-26 14:20:05.752 11101-11101/com.android.press.press W/Syste m.err: at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.press.press.LoginActivity.getFacebookProfilePicture(LoginActivity.java:215) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.press.press.LoginActivity$1$1.onCompleted(LoginActivity.java:85) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.facebook.GraphRequest$5.run(GraphRequest.java:1379) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 05-26 14:20:05 .752 11101-11101/com.android.press.press W/System.err: at android.os.Looper.loop(Looper.java:135) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5221) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at java.lang.reflect.Method.invoke(Native Method) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 05-26 14:20:05.752 11101-11101/com.android.press.press W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Please let me know if im doing something wrong.I have tried a variety of sources but this seems to be the most popular answer. I would greatly appreciate the help. Thanks in advance

Instead of using an input stream as used in this line

bitmap = BitmapFactory.decodeStream(facebookProfileURL.openConnection().getInputStream());

You should use Picasso Library to show images in ImageView, passing the url (without initiating any connection and without using the input stream, but only using the url that was received from Facebook):

Picasso.with(this).load(/* url of image */).into(/*your imageview id*/);

as shown in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM