Ajeet
Ajeet

Reputation: 1560

Unable to fetch profile pic using graph api Facebook sdk 4.0

I am unable to fetch image using

 Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/100001220051873/picture");
            if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
            else
                Toast.makeText(getActivity(),"null it is",Toast.LENGTH_SHORT).show();

Where getBitmapFromURL is

public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

But when I use below everything works fine

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

But I have read there Strict mode in android 2.2 that this policy is not safe to use and not just this, it is also blocking my main UI for few seconds.

I want to know first, why I am unable to fetch facebook profile pic without using above policy and Is there any way that I can avoid above policy and may able to fetch my profile pic? Thanks in advance :)

Upvotes: 0

Views: 1138

Answers (1)

Shashank Srivastava
Shashank Srivastava

Reputation: 446

Following is the working code:

GraphRequest request = GraphRequest.newMeRequest(
            accessToken,
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {
                    // Application code
                    Log.d("response","response"+object.toString());
                    Intent intent=new Intent(MainActivity.this, ProfilePictureActivity.class);
                    try {
                        intent.putExtra("pic_url",object.getJSONObject("picture").getJSONObject("data").getString("url"));

                        startActivity(intent);
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email,gender,birthday,picture.width(300)");
    request.setParameters(parameters);
    request.executeAsync();

Upvotes: 4

Related Questions