0

Tenho o seguinte método para efetuar um SilentLogin com o google +

 private void silentLogin() throws MalformedURLException {
        OptionalPendingResult<GoogleSignInResult> pendingResult = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (pendingResult != null) {
            if (pendingResult.isDone()) {
                GoogleSignInResult signInResult = pendingResult.get();

                profile_pic = new **URL("https://plus.google.com/s2/photos/profile/" + signInResult.getSignInAccount().getId() + "?sz=100");** //url para image
                new LoadImage().execute();

            } else {
                pendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                    @Override
                    public void onResult(GoogleSignInResult googleSignInResult) {
                        GoogleSignInResult signInResult = googleSignInResult;

                        try {
                            profile_pic = new URL("https://plus.google.com/s2/photos/profile/" + signInResult.getSignInAccount().getId() + "?sz=100");
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }
                        new LoadImage().execute();
                    }
                });
            }
        } else {
            progressBar.setVisibility(View.GONE);
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        }
    }

E eu tenho a seguinte classe para obter a imagem

private class LoadImage extends AsyncTask {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Bitmap mIcon_val = BitmapFactory.decodeStream(profile_pic.openConnection().getInputStream());
                UVSingleton.getInstance().setProfilePicture(mIcon_val);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setVisibility(View.GONE);
                    }
                });
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

Porém tenho problema com a url que eu crio pois a mesma me retorna que a foto nao existe.

Qual a forma correta de obter a foto do perfil do google + ?

2 Respostas 2

0

Eu já usei código abaixo e funcionou perfeitamente.

private void getProfileInformation() {
    try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

            Log.e(TAG, "Name: " + personName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + personPhotoUrl);

            txtName.setText(personName);
            txtEmail.setText(email);

            // by default the profile url gives 50x50 px image only
            // we can replace the value with whatever dimension we want by
            // replacing sz=X
            personPhotoUrl = personPhotoUrl.substring(0,
                    personPhotoUrl.length() - 2)
                    + PROFILE_PIC_SIZE;

            new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);

        } else {
            Toast.makeText(getApplicationContext(),
                    "Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Background Async task to load user profile picture from url
 * */
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);
    }
}

Qualquer outra duvida consulte o link do tutorial.

http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

0

Resolvi meu problema da seguinte forma:

GoogleSignInAccount acct = result.getSignInAccount();
String url = "http://" + acct.getPhotoUrl().getAuthority() + acct.getPhotoUrl().getPath() + "?sz=76";

E para baixar a imagem utilizei a lib Picasso e passei a url obtida;

 Picasso.with(this).load(url).into(profilePhoto);

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .