我想整合谷歌加登錄來獲取用戶的詳細信息,如姓名,電子郵件和他的個人資料圖片。發送圖像從一個活動到另一個失敗
現在使用下面的代碼,我想,如果我用它在同一個活動,我讓他的個人檔案相片太
Login.Java
得到他的姓名,電子郵件和個人檔案相片public void onConnected(Bundle connectionHint) {
// We've resolved any connection errors. mGoogleApiClient can be used to
// access Google APIs on behalf of the user.
// Get user's information
getProfileInformation();
}
private void getProfileInformation() {
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();
Toast.makeText(this, personPhotoUrl, Toast.LENGTH_LONG).show();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
//new GetProfileImage(urImageView).execute(personPhotoUrl);
// Create the bundle
new GetProfileImage().execute(personPhotoUrl);
Bundle bundle = new Bundle();
// Add your data from getFactualResults method to bundle
bundle.putString("Google", "Logged in using Google Account");
bundle.putString("GoogleUsername", currentPerson.getDisplayName());
bundle.putString("GoogleEmail", email);
if(resultBmp!=null) {
i.putExtra("GoogleProfileImage", resultBmp);
}
i.putExtras(bundle);
startActivity(i);
}
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);
}
}
MainActivity.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, PlusOptions.builder().build())
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
mGoogleApiClient.connect();
Intent intent = getIntent();
if(intent.getStringExtra("Google") != null){
// 1. get passed intent
// 2. get message value from intent
String userName = intent.getStringExtra("GoogleUsername");
String email = intent.getStringExtra("GoogleEmail");
if(intent.getStringExtra("Google").equals("Logged in using Google Account")){
((TextView)findViewById(R.id.txtUser)).setText(userName);
((TextView)findViewById(R.id.txtemail)).setText(email);
Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("GoogleProfileImage");
//Bitmap bitmap = getIntent().getParcelableExtra("GooglePic");
ImageView imageView = (ImageView) findViewById(R.id.imgProfilePic);
imageView.setImageBitmap(bitmap);
}
}
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
Log.d("Debug","Connection failed");
Intent i = new Intent(this,Login.class);
startActivity(i);
finish();
//super.onConnectionFailed(result);
}
@Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
Log.d("Debug","Connected");
//super.onConnected(connectionHint);
mGoogleApiClient.connect();
}
如果我嘗試這個圖像發送到n ext activity it dosent在第一次登錄時向我顯示圖片。如果我第二次登錄,或者我恢復應用程序,它會顯示圖片。
任何人都可以說我在哪裏我的MainActivity錯了嗎?
我認爲更好的方法是通過ImageView路徑並從MainActivity路徑中獲取圖像,傳遞圖像bitamp需要大量內存。 – 2014-11-22 11:29:26
是否有任何理由通過圖像位圖,而不是路徑? – 2014-11-22 11:32:00
沒有什麼這樣的。我只是在用戶登錄後才顯示用戶的個人資料照片,就像使用導航抽屜的gmail和youtube一樣。 – coder 2014-11-22 11:34:54