You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中图片上传后如何更新ProfileActivity的头像显示

Hey Marcus, let's figure out why your avatar isn't updating until you restart the app! The issue here is Picasso's caching mechanism—since you're using the user ID as the image filename, the URL stays the same even after you upload a new avatar. Picasso sees the same URL and loads the cached old image instead of fetching the new one from the server.

Here are a few straightforward fixes you can implement:

1. Refresh on Resume & Clear Picasso Cache

First, make sure your upDateInfo() runs every time the user returns to the ProfileActivity (not just on create). Then, tell Picasso to invalidate the cached image for that URL before loading it again:

@Override
protected void onResume() {
    super.onResume();
    upDateInfo(); // Refresh every time we come back to this screen
}

private void upDateInfo() {
    if (Conts.USERINFO != null) {
        String subName = firstTwoChar(Conts.USERINFO.getName());
        final int radius = 30;
        final int margin = 1;
        final RoundedCornersTransformation transformation = new RoundedCornersTransformation(radius, margin);
        
        String avatarUrl = "http://www.server.com/uploads/avatars/" + Conts.USERINFO.getId();
        
        // Clear the cached image for this URL so Picasso fetches the new one
        Picasso.get().invalidate(avatarUrl);
        
        // Load the fresh image from the server
        Picasso.get()
                .load(avatarUrl)
                .transform(transformation)
                .into(p_avatar);
        
        etFullname.setText(Conts.USERINFO.getName().toLowerCase());
        etFullname.setEnabled(false);
        etEmail.setText(Conts.USERINFO.getEmail());
        etEmail.setEnabled(false);
        btnUpdate.setVisibility(View.INVISIBLE);
    }
}

2. Add a Version/Timestamp to the Image URL

Another approach is to modify the URL to include a unique identifier (like a version number or timestamp) that changes when the avatar is updated. This tricks Picasso into treating it as a new image:

Option A: Use a timestamp (quick test fix)

String avatarUrl = "http://www.server.com/uploads/avatars/" + Conts.USERINFO.getId() + "?t=" + System.currentTimeMillis();
Picasso.get().load(avatarUrl).transform(transformation).into(p_avatar);

Note: This will load the image fresh every time, which works but adds extra network calls.

Option B: Use an avatar version (better for production)

  • Add an avatarVersion field to your USERINFO model.
  • When you successfully upload a new avatar, increment this version on the server and update Conts.USERINFO with the new value.
  • Then use it in the URL:
String avatarUrl = "http://www.server.com/uploads/avatars/" + Conts.USERINFO.getId() + "?v=" + Conts.USERINFO.getAvatarVersion();
Picasso.get().load(avatarUrl).transform(transformation).into(p_avatar);

This way, Picasso only fetches a new image when the version changes, balancing freshness and performance.

3. Ensure Update Success Triggers a Refresh

Make sure when your profilePresenter.dataListAPI call succeeds, you update Conts.USERINFO (if needed) and call upDateInfo() to refresh the UI immediately:

@Override
public void onUpdateSuccess() {
    // Assuming your API returns the updated user info, update Conts.USERINFO here
    // Conts.USERINFO = updatedUserInfo;
    
    // Refresh the profile UI before navigating away
    upDateInfo();
    
    // Navigate to the new Activity
    startActivity(new Intent(this, YourNewActivity.class));
}

Pick the solution that fits your workflow best—clearing the cache (option 1) is the quickest fix, while using a version number (option 2B) is more efficient for production.

内容的提问来源于stack exchange,提问作者Marcus Barnet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:19:28