Search code examples
androidfacebook-graph-apiandroid-imageviewandroid-bitmapprofile-picture

Converting Facebook ProfilePictureView to bitmap to use in ImageView


I am trying to convert the profile image returned from Facebook and change it into a bitmap so that I can use it in a method that requires a bitmap. I'm able to get the profile picture by using the code below and put it into the ProfilePictureView. I also created two other views for testing...a CircleImageView and an ImageView. So far only the ProfilePictureView shows the image. Here is the example of the code I tried, although I have tried several methods I've found on the web to create the bitmap and none have worked. Any suggestions/solutions would be greatly appreciated!

    ProfilePictureView profilePictureView;
    profilePictureView = (ProfilePictureView) findViewById(R.id.facebookPictureView);
    profilePictureView.setProfileId(userId);  //this works to get and set the profile pic

    standardImageView = (ImageView) findViewById(R.id.imageView);
    circleImageView = (CircleImageView) findViewById(R.id.profImage);

    AsyncTask<Void, Void, Bitmap> t = new AsyncTask<Void, Void, Bitmap>() {
        protected Bitmap doInBackground(Void... p) {
            Bitmap bmp = null;
            try {
                URL aURL = new URL("http://graph.facebook.com/" + userId + "/picture?type=large");
                URLConnection conn = aURL.openConnection();
                conn.setUseCaches(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bmp = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bmp;
        }

        protected void onPostExecute(Bitmap bmp) {

            standardImageView.setImageBitmap(bmp);
            circleImageView.setImageBitmap(bmp);
        }
    };
    t.execute();

Using the debugger and stepping through, I have found the bmp is null. I've tried a few other methods and the same thing has happened. Help!


Solution

  • You could also decide to use Glide as well, a better option for Picasso

    Bitmap theBitmap = null;
    theBitmap = Glide.
          with(getActivigetApplicationContext()).
          load("your image url here").
          asBitmap().
          into(-1, -1).
          get();