Saruulbat
Saruulbat

Reputation: 321

How to get Facebook profile picture in android?

img_url = new URL("http://graph.facebook.com/" + user.getId() + "/picture?type=large");
                            InputStream is = img_url.openConnection().getInputStream();
                            Bitmap avatar;
                            avatar = BitmapFactory.decodeStream(is);
                            ivAvatar.setImageBitmap(avatar);

When i getting facebook profile picture, following error occured.

android.os.NetworkOnMainThreadException

How to solve it?

Upvotes: 3

Views: 1185

Answers (2)

Luo Lei
Luo Lei

Reputation: 141

Profile profile = Profile.getCurrentProfile();

Uri uri = profile.getProfilePictureUri(72, 72); //72 is the height and width of the profile pic, facebook will clip it for you.

new Thread(new Runnable() {
    @Override
    public void run() {
        try{
            URL newURL = new URL(uri.toString());
            Bitmap profilePic = BitmapFactory.decodeStream(newURL.openConnection().getInputStream());}
        catch (IOException e)
        {e.printStackTrace();}
    }
}).start();

This works for me.

Upvotes: 0

James
James

Reputation: 962

Your edited(removed) part of your question (for reference)

You're missing the second / in http:// http://graph.facebook.com/100001119045663/picture?type=large.

 java.net.UnknownHostException: http:/graph.facebook.com/100001119045663/picture?type=large

The java.net.UnkownHostException describes that it can't access the URL, either a connectivity problem or malformed/invalid URL.

Second Part- Question Add NetworkOnMainThread is quite descriptive. If you're calling functions from onCreate, onResume etc, you are executing the code on the UI thread. What this means is if you're crunching code, you can freeze the UI. You will need to create a seperate Task or Thread. For more information on what I am saying and how to implement a solution, please refer to http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

TLDR Version...

package de.vogella.android.asynctask;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import de.vogella.android.asyntask.R;

 import android.app.Activity;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;

public class ReadWebpageAsyncTask extends Activity {
      private TextView textView;

   @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    HTTPExample task = new HTTPExample ();
    task.execute(new String[] { "http://pictureurl.com/image.jpg" });
  }

  private class HTTPExample extends AsyncTask<String, Void, String> {
         @Override
        protected String doInBackground(String... urls) {
        //urls is an array not a string, so iterate through urls.
        //Get Picture Here - BUT DONT UPDATE UI, Return a reference of the object
        return response;
        }

         @Override
         protected void onPostExecute(String result) {
         //Update UI
         Log.i("Result", result);
       }
   }


} 

Upvotes: 5

Related Questions