2016-02-12 51 views
1

的完整URL我使用谷歌登錄在Android應用並保存在數據庫一個鏈接到他們的資料圖片URL獲取用戶的谷歌頭像

在過去,我會得到完整的URL,開始與這一個的這些,例如:

https://lh3.googleusercontent.com/{random stuff}/photo.jpg 
https://lh4.googleusercontent.com/{random stuff}/photo.jpg 
https://lh5.googleusercontent.com/{random stuff}/photo.jpg 
https://lh6.googleusercontent.com/{random stuff}/photo.jpg 

自從谷歌播放更新這(我認爲在8.3〜)我只得到

/{random stuff}/photo.jpg 

這顯然不會鏈接到圖片。

這裏是我的代碼:

GoogleSignInAccount acct = result.getSignInAccount(); 

    if (acct != null) 
    { 
     String profilePicPath = ""; 
     if (acct.getPhotoUrl() != null) { 
      profilePicPath = acct.getPhotoUrl().getPath(); 
     } 
    } 

我在做什麼錯?

編輯:我相信我在做錯的是我在URL後加了getPath()

+2

是的,刪除getPath()會給你完整的url。 –

回答

0

您需要在Google控制檯上啓用G + API。

enter link description here

您需要初始化GoogleApiClient

enter link description here

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ...... 
    googleApiClient = new GoogleApiClient.Builder(getActivity()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Plus.API) 
      .addScope(Plus.SCOPE_PLUS_LOGIN) 
      .addScope(Plus.SCOPE_PLUS_PROFILE) 
      .build(); 
} 
@Override 
public void onConnected(Bundle bundle) { 

    Plus.PeopleApi.loadVisible(googleApiClient, null).setResultCallback(this); 



    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) { 
     Person person = Plus.PeopleApi.getCurrentPerson(googleApiClient); 
     personNameView.setText(person.getDisplayName()); 
     if (person.hasImage()) { 

      Person.Image image = person.getImage(); 


      new AsyncTask<String, Void, Bitmap>() { 

       @Override 
       protected Bitmap doInBackground(String... params) { 

        try { 
         URL url = new URL(params[0]); 
         InputStream in = url.openStream(); 
         return BitmapFactory.decodeStream(in); 
        } catch (Exception e) { 
        /* TODO log error */ 
        } 
        return null; 
       } 

       @Override 
       protected void onPostExecute(Bitmap bitmap) { 
        personImageView.setImageBitmap(bitmap); 
       } 
      }.execute(image.getUrl()); 
     } 
    } 

enter link description here

希望這有助於

+0

我可能是錯的,但這不是這樣做的舊方法嗎?我不認爲他們再使用'Plus'的東西了?我認爲這是我以前做的,它運作良好;然後他們在幾個月或幾周前改變了事情。 – KickingLettuce

1

的問題是這樣的:

我加了getPath()這裏profilePicPath = acct.getPhotoUrl().getPath();。相反,我刪除了它,獲得了它的字符串形式,這就是我所需要的。

相關問題