获取Google plus帐户的个人资料照片

时间:2014-11-20 05:02:33

标签: android image android-activity google-plus user-profile

我正在尝试访问Google +用户个人资料并获取其详细信息,如姓名,电子邮件和个人资料照片,如下所示:

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

}

我能够得到这个名字,电子邮件却无法获得他的个人资料照片。

这就是我将个人资料照片发送到我的下一个活动的方式:

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

有谁能说我如何获取个人资料照片并将其发送到我的下一个活动?

2 个答案:

答案 0 :(得分:1)

您正在从服务器获取图像,因此您需要使用AsyncTask ..

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

现在调用此函数

 new GetProfileImage().execute(personPhotoUrl);

而不是

 Bitmap image = BitmapFactory.decodeFile(personPhotoUrl);

现在将此resultBmp命名位图传递给下一个活动。

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

答案 1 :(得分:0)

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

            }

然后是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);
    }
}

它对我有用。