Issue
I am downloading image from firebase in async task and then place it in a imageview and till here, all is fine. In onPostExecute() method, I want to get the drawable from the imageview I put image in in the first step but it throws exception as it imageview contains nothing.
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);
}
}
I do not have any problem with downloading, it downloads and puts image in imageview correctly. I though, I am better off downloading image in main treat not in async task but not sure if it is okay.
Download image function
private void downloadProfileImage() {
downloadImage = new DownloadImage("image_" + id + ".jpg", storageReference);
downloadImage.download(image);
}
Download Class
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());
}
});
}
}
Solution
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() {
// ...
}
});
Answered By - Athira
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.