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
avatarVersionfield to yourUSERINFOmodel. - When you successfully upload a new avatar, increment this version on the server and update
Conts.USERINFOwith 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

