在Android中获取新的Google+个人资料图片

时间:2015-12-07 12:22:20

标签: android google-plus

在我的应用中,用户可以使用他/她的Google+帐户登录。 除此之外,我们还会获取用户的个人资料图片。

我们发现,如果用户在网络上更改了他的照片,则此更改不会反映在手机上。我想这是因为退出我们的应用程序并未退出Google,因此无法刷新。

有没有办法获取最新的图片。我宁愿避免"解决问题"这可能在将来不起作用。

2 个答案:

答案 0 :(得分:0)

因此,我们假设我们有一个登录按钮,该按钮启动首先验证我们的方法,当然还会向我们提供需求数据login.setOnClickListener(view -> syncGoogleAccount());

以及将我们登录的方法,我们将首先使用此方法遍历我们的电子邮件地址应用:

public String findGmailAccount() {

    AccountManager am = AccountManager.get(context);
    Account[] accounts = am.getAccounts();

    if (accounts.length > 0) {
        for (int j = 0; j < accounts.length; j++) {
            Account account = accounts[j];
            if (account.type != null && account.type.equals("com.google")) {

                String email = account.name;
                return email;

            } else

            {

                return null;

            }
        }
    }


    return null;
}


 public void syncGoogleAccount() {

    if (isNetworkAvailable()) {
        String accountarrs = findGmailAccount();
        if (accountarrs != null) {
            //you can set here account for login
            getTask(register.this, accountarrs, SCOPE).execute();

        } else {
            Toast.makeText(mContext, "No Google Account Sync!",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(mContext, "No Network Service!",
                Toast.LENGTH_SHORT).show();
    }
}

在进行任何网络操作之前​​检查连接是很重要的,因此我们首先检查连接性。

 public boolean isNetworkAvailable() {

    ConnectivityManager cm = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        Log.e("Network Testing", "***Available***");
        return true;
    }
    Log.e("Network Testing", "***Not Available***");
    return false;
}

然后如果我们登录,我们会调用我们的信息getter方法来获取包含img url的信息

    public static  void getUserInformationFromAccount() {
    try {
        System.out.println("On Home Page***\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
                + AbstractGetNameTask.GOOGLE_USER_DATA);
        JSONObject profileData = new JSONObject(
                AbstractGetNameTask.GOOGLE_USER_DATA);

        if (profileData.has("picture")) {
            String userImageUrl = profileData.getString("picture");
            new GetImageFromUrl().execute(userImageUrl);
        }

        if (profileData.has("name")) {
            myname = profileData.getString("name");
            userDetails.setName(myname);


        }
        if (profileData.has("gender")) {
            gender = profileData.getString("gender");

        }
        if (profileData.has("birthday")) {
             birthday = profileData.getString("birthday");

        }


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我们的图片链接async,可下载我们的图片/用户个人资料图片

 public static class GetImageFromUrl extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        profilepic.setImageBitmap(result);
        CommonMethods.saveUserImageToSd(result, Constant.GOOGLE_IMAGE, Constant.DISPLAY_IMAGE);
    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

答案 1 :(得分:0)

相同个人资料图片背后的原因可能是存储图像缓存。尝试从您正在使用的图像加载库中删除缓存。我使用Universal Image Loader库,它可以选择删除缓存。

试试这可能会对你有所帮助。

我可以知道您用于图像加载的库吗?

编辑:

如果你想获得Profile Pic,那么点击URL即 请求网址

https://www.googleapis.com/plus/v1/people/10343557736...774874?fields=image&key={YOUR_API_KEY}

您只需传递UserId和API密钥即可。在那之后你会得到响应。

{
 "image": {
  "url": "https://lh3.googleusercontent.com/-xIeb3BMYFMU/AAAAAAAAAAI/AAAAAAAAAhU/gQOwkKFA2OE/photo.jpg?sz=50",
  "isDefault": false
 }
}

您可以根据需要更改尺寸,例如sz=150

当您使用该URL时,请确保缓存已从图像加载库中关闭。之后,每当您获取用户图像时,它将反映最新的配置文件图像,而不是存储在缓存中的先前图像。