2014-11-22 90 views
0

我想整合谷歌加登录来获取用户的详细信息,如姓名,电子邮件和他的个人资料图片。发送图像从一个活动到另一个失败

现在使用下面的代码,我想,如果我用它在同一个活动,我让他的个人档案相片太

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错了吗?

+0

我认为更好的方法是通过ImageView路径并从MainActivity路径中获取图像,传递图像bitamp需要大量内存。 – 2014-11-22 11:29:26

+0

是否有任何理由通过图像位图,而不是路径? – 2014-11-22 11:32:00

+0

没有什么这样的。我只是在用户登录后才显示用户的个人资料照片,就像使用导航抽屉的gmail和youtube一样。 – coder 2014-11-22 11:34:54

回答

0

尝试这样的方式,

将其转换为一个字节数组,你将它添加到以前的意图,把它发送出去,和解码。

//转换为字节数组

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

Intent in1 = new Intent(this, Activity2.class); 
in1.putExtra("image",byteArray); 

然后在活动2:

byte[] byteArray = getIntent().getByteArrayExtra("image"); 
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
+0

创建字节数组ia不是一个正确的解决方案,因为从图像创建字节需要一定的时间,这可能导致延迟显示下一个屏幕。通过Intent传递对象时,可以使用parcelable接口。请阅读更多关于parcelable from developer.android.com – 2014-11-22 11:30:18

+0

@ Moradiya-感谢您的帮助。即使我试过这种方式,但没有工作 – coder 2014-11-22 11:40:50

0

让你的位图引用静态为静态变量可以从另一个活动进行访问。

0

我的事情你的问题是:

加载时第一次活动开始的形象还不加载图像需要时间,因此,这是这样你看到的图像仅在第二次时,已经加载。

尝试使用asyncTask或任何其他后台进程来加载图像。

如果您使用普通线程,请在设置要查看的图像时不要忘记调用“runOnUIthread”。 如果您已经使用后台进程加载图像,则在进程完成时设置回调来调用。

相关问题