简体   繁体   English

使用 AsyncTask 更新 Android 中的用户配置文件图像

[英]Using an AsyncTask to update a user profile image in Android

I have a problem with the user profile picture update in my android application.我的 android 应用程序中的用户个人资料图片更新出现问题。

Introduction介绍

In my MainActivity I can switch between two Fragments, the first one is the HomeFragment and the second is the UserProfileFragment .在我的MainActivity中,我可以在两个 Fragment 之间切换,第一个是HomeFragment ,第二个是UserProfileFragment In the UserProfile, the user can decide to upload a new image, so i let the user pick the image from local storage, then i upload this picture into FirebaseStorage and get the download link.在 UserProfile 中,用户可以决定上传一张新图片,所以我让用户从本地存储中选择图片,然后将此图片上传到FirebaseStorage并获取下载链接。 I use a class called LoadImageTask extending an AsyncTask that download the image from url and then upload the resulting bitmap into an ImageView that i have in my UserProfileFragment . I use a class called LoadImageTask extending an AsyncTask that download the image from url and then upload the resulting bitmap into an ImageView that i have in my UserProfileFragment .

The problem:问题:

When a user uploads a new profile picture, LoadImageTask does his job correctly.当用户上传新的个人资料图片时, LoadImageTask会正确完成他的工作。 The problem is that, when the user tries to update his profile picture, the ImageView is still showing the old image and the user must go to HomeFragment and then go back to the UserProfileFragment to see the update.问题是,当用户尝试更新他的个人资料图片时, ImageView仍然显示旧图像,用户必须 go 到HomeFragment ,然后 Z34D1F91FB2E514B8576FAB1A75A89A6B 更新到UserProfileFragment So, basically the update works but the Profile Picture change is not showed immediately.因此,基本上更新有效,但个人资料图片更改不会立即显示。

What i tried to do我试图做什么

I tried many things like invalidating the imageView or trying to force the imageView in other ways but it didn't worked.我尝试了很多方法,例如使imageView无效或尝试以其他方式强制imageView ,但没有奏效。

 public class LoadImageTask extends AsyncTask<String, Void, Bitmap> { private ImageView image; public LoadImageTask(ImageView image) { this. image = image; } @Override protected Bitmap doInBackground(String... urls) { String downloadUrl = urls[0]; Bitmap bitmap = null; try { java.net.URL url = new java.net.URL(downloadUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(input); } catch (IOException e) { e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { if(result.= null) { image;setImageBitmap(circleTransformation(result)); } } }

When you pick image from the local storage you can set image to imageView from the uri in your onActivityResult.当您从本地存储中选择图像时,您可以从 onActivityResult 中的 uri 将图像设置为 imageView。

imageView.setImageURI(imgUri);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM