How to get Facebook profile photo on 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 get the facebook profile picture, an error has occurred.

android.os.NetworkOnMainThreadException

How to solve it?

+3
source share
2 answers

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

You are missing a second / at http: // http://graph.facebook.com/100001119045663/picture?type=large .

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

java.net.UnkownHostException describes that it cannot access the URL, either the connection problem, or the invalid / invalid URL.

-  NetworkOnMainThread . onCreate, onResume .., . , , . . , , http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

TL;DR...

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


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

.

0

All Articles