coder
coder

Reputation: 13248

Get profile pic of Google plus account

I'm trying to access google+ user profile and getting his details like name, email and profile pic as shown:

public void onConnected(Bundle connectionHint) {
// We've resolved any connection errors. mGoogleApiClient can be used to
// access Google APIs on behalf of the user.
mSignInClicked = false;

if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {

    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    Person currentPerson = Plus.PeopleApi
            .getCurrentPerson(mGoogleApiClient);
    String personPhotoUrl = currentPerson.getImage().getUrl();
    String personGooglePlusProfile = currentPerson.getUrl();
    personPhotoUrl = personPhotoUrl.substring(0,
            personPhotoUrl.length() - 2)
            + PROFILE_PIC_SIZE;
    String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
    Bitmap image = BitmapFactory.decodeFile(personPhotoUrl);


    i.putExtra("Google", "Logged in using Google Account");
    i.putExtra("GoogleUsername", currentPerson.getDisplayName());
    i.putExtra("GoogleEmail", email);
    i.putExtra("GoogleProfileImage", image);
    startActivity(i);

}

I'm able to get the name,email but unable to get his profile pic.

This is how I'm sending the profile pic to my next activity:

 Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("GoogleProfileImage");
 ImageView imageView = (ImageView) findViewById(R.id.imgProfilePic);
 imageView.setImageBitmap(bitmap);

Can anyone say me how do I get the profile pic and send it to my next activity ?

Upvotes: 0

Views: 864

Answers (2)

Jay Rathod
Jay Rathod

Reputation: 11245

if (mPlusClient.isConnected()) {

                userImage = (ImageView) mGet_user_data
                        .findViewById(R.id.imageView1);


                Person currentPerson = ((PlusClient) mPlusClient)
                        .getCurrentPerson();

                showImage = currentPerson.getImage().getUrl();




                showImage = showImage.substring(0, showImage.length() - 2)
                        + PROFILE_PIC_SIZE;
                new LoadProfileImage(userImage).execute(showImage);
                mGet_user_data.show();

            } else {

                Toast.makeText(getApplicationContext(), "Please Sign In",
                        Toast.LENGTH_LONG).show();

            }

Then LoadImageProfile

 private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public LoadProfileImage(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

it's working well for me.

Upvotes: 0

Piyush
Piyush

Reputation: 18933

You are getting image from server so you need to use AsyncTask..

Declare one global variable for Bitmap

Bitmap resultBmp;

private class GetProfileImage extends AsyncTask<String, Void, Bitmap> {

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {

            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        resultBmp = result;
        //bmImage.setImageBitmap(result);
    }
}

Now call this function

 new GetProfileImage().execute(personPhotoUrl);

instead of this

 Bitmap image = BitmapFactory.decodeFile(personPhotoUrl);

Now pass this resultBmp named bitmap to your next Activity.

 if(resultBmp!=null) {
     i.putExtra("GoogleProfileImage", resultBmp);
 }

Upvotes: 1

Related Questions