我正在异步任务中从firebase下载图像,然后将其放置在图像视图中,直到这里,一切都很好。在onPostExecute((方法中,我想从我在第一步中放入的imageview中获取drawable,但它抛出了异常,因为它的imageview不包含任何内容。
public class DownloadPhoto extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
downloadImage();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
Palette colorPalette = Palette.from(bitmap).generate();
int darkVibrantColor = colorPalette.getDarkVibrantColor(Color.parseColor("#546369"));
title.setTextColor(darkVibrantColor);
details.setTextColor(darkVibrantColor);
}
}
我下载没有任何问题,它下载并将图像正确地放在imageview中。不过,我最好在主处理中下载图像,而不是在异步任务中,但不确定是否可以。
下载图像功能
private void downloadProfileImage() {
downloadImage = new DownloadImage("image_" + id + ".jpg", storageReference);
downloadImage.download(image);
}
下载类
public class DownloadImage {
private String name;
private StorageReference storageReference;
public DownloadImage(String name, StorageReference storageReference) {
this.name = name;
this.storageReference = storageReference;
}
public void download(final ImageView ımageView) {
StorageReference sRef = storageReference.child("images/" + name);
sRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(ımageView);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("resultDownload", e.getLocalizedMessage());
}
});
}
}
Try this code
检查onPostExecute((中的图像是否已初始化并引用了正确的imageView。