我正在异步任务中从Firebase下载图像,然后将其放置在imageview中,直到这里一切都很好。在onPostExecute()方法中,我想从第一步中放入图像的imageview中获取可绘制对象,但由于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());
}
});
}
}
答案 0 :(得分:2)
Try this code
Picasso mPicasso = Picasso.with(this);
mPicasso.load(imageUrl)
.into(image1, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
Drawable drawable = image1.getDrawable();
// ...
}
@Override
public void onError() {
// ...
}
});
答案 1 :(得分:0)
检查onPostExecute()中的图像是否已初始化并引用了正确的imageView。