2

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!

Wayne Johnson
  • 214
  • 3
  • 18

3 Answers3

2

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();
Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61
0
 Uri imageUri;
  String urlImage = null;
 String id = null;

// this method is use to get id of user

 GraphRequest request = GraphRequest.newMeRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            try {

                            id =  object.getString("id"));

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle params = new Bundle();
            params.putString("fields", "id,name,link,picture");
            request.setParameters(params);
            request.executeAsync();

// this method is use to get profile picture of user and placing a image in image view using Picasso . while using this you don't have to get bitmap object , Picasso will directly convert string into bitmap

urlImage = "https://graph.facebook.com/" + id + "/picture?width=" + AppController.convertDpToPx(60);
                    Log.d("url", urlImage);
                    imageUri = Uri.parse(urlImage);
                    if (!urlImage.isEmpty()) {
                        Picasso.with(getApplicationContext())
                                .load(urlImage)
                                .resize(AppController.convertDpToPx(60), 0)
                                .into(mTarget);
                    }



 private Target mTarget = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {

            mibProfileButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mibProfileButton.setImageBitmap(getRoundedCornerBitmap(bitmap, bitmap.getWidth() * 17/60));

        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            AppController.getInstance().showError(R.string.error_image, R.drawable.repeating_bg_error);
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {

        }
    };
Ankita Chopra
  • 329
  • 4
  • 12
0

Use this Snippet on bitbucket

you can treat it like a library

Elltz
  • 10,730
  • 4
  • 31
  • 59