【问题标题】:Get profile pic of Google plus account获取 Google+ 帐户的个人资料图片
【发布时间】:2014-11-20 05:02:33
【问题描述】:

我正在尝试访问 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);

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

【问题讨论】:

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


    【解决方案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 命名位图传递给您的下一个 Activity。

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

    【讨论】:

    • @PiyushGupta-是的,这是真的..我同意,但我的主要活动中没有该图像..我需要将该图像传递给下一个活动。你能告诉我我该怎么做?
    • @coder 好的。所以你只需要删除这个构造函数 public GetProfileImage(ImageView bmImage) { this.bmImage = bmImage; }
    • 仍然无法获取图片
    • 是的 piyush..无法在下一个活动中得到它
    【解决方案2】:
    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();
    
                }
    

    然后加载图像配置文件

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

    对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      相关资源
      最近更新 更多