2014-11-22 3 views
0

이름, 이메일 및 프로필 사진과 같은 사용자 세부 정보를 얻기 위해 google plus login을 통합하려고합니다.하나의 활동에서 다른 이미지로 이미지 전송 실패

지금은 내가 자신의 프로필 그림

너무

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 활동입니다. 두 번째로 로그인하거나 앱을 다시 시작하면 사진이 표시됩니다.

누구나 내 MainActivity에서 어디서 잘못 될 수 있습니까?

+0

내가 더 좋은 방법은 imageview 경로를 전달하고 MainActivity의 경로에서 이미지를 가져 오는 것, 이미지 전달을 통과하는 것은 많은 메모리가 필요하다고 생각합니다. –

+0

경로 대신 이미지 비트 맵을 전달할만한 이유가 있습니까? –

+0

그 밖에 아무것도 없습니다. 나는 gmail 및 youtube와 같이 탐색 창에 로그인 한 후 사용자에게 자신의 프로필 사진을 보여줄 수 있습니다. – coder

답변

0

당신이 의도에 추가 발송 및 디코딩하기 전에

는 바이트 배열로 변환, 이런 식으로 같은 해보십시오.

는 그런 활동 2 바이트 배열

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

로 변환 // : 정적 변수가 다른 활동에서 액세스 할 수있는

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

다음 화면을 표시 할 때 지연을 초래할 수있는 이미지에서 바이트를 만드는 데 시간이 걸리기 때문에 올바른 해결책이 아닌 바이트 배열 만들기. 인 텐트를 통해 객체를 전달하는 동안 parcelable 인터페이스를 사용할 수 있습니다. developer.android.com에서 parcelable에 대한 자세한 내용을 읽어보십시오. –

+0

@ Moradiya- 도움을 주셔서 감사합니다. 비록이 방법을 시도했지만 didnt은 효과가있었습니다. – coder

0

정적 비트 맵 참조하십시오.

0

나는 것은 문제가 있다는 것입니다 :

처음으로 활동을 시작 이미지가 아직로드되지 않은 경우 이미지가 너무 시간이 걸릴로드, 그 유는 두 번째 시간에 이미지를 볼 방법 때를 이미로드되었습니다.

asyncTask 또는 다른 백그라운드 프로세스를 사용하여 이미지를로드 해보십시오.

일반 스레드를 사용하는 경우 볼 이미지를 설정할 때 "runOnUIthread"를 호출하는 것을 잊지 마십시오. 이미 백그라운드 프로세스를 사용하여 이미지를로드 한 경우 프로세스가 완료되면 콜백 콜을 설정하십시오.

관련 문제