'Bitmap is null when being observed two times by Live Data
I have ImageView imgProfil in MojProfilActivity and I want to load a picture from a database there. It works just fine the first time, but after I try it the second time the bmp is null for some strange reason. Can you help, please?
MutableLiveData<ResponseBody> mld = UserRepository.getInstance(MainApplication.apiManager).GetProfilna(Utils.getInstance().getKorisnik().getId());
mld.observe(MojProfilActivity.this, new Observer<ResponseBody>() {
@Override
public void onChanged(ResponseBody responseBody) {
bmp= BitmapFactory.decodeStream(responseBody.byteStream());
imgProfil.setImageBitmap(bmp);
}
});
This is my interface:
public interface RestInterface {
@GET("users/getprofilna/{UserId}")
Call<ResponseBody> GetProfilna(@Path("UserId") String UserId);
}
This is my Api Manager
public class ApiManager {
private static RestInterface service;
private static ApiManager apiManager;
private ApiManager(){
service = RetrofitService.Create();
}
public static ApiManager getInstance(){
if(apiManager == null) {
apiManager = new ApiManager();
}
return apiManager;
}
public void GetProfilna(String UserId, Callback<ResponseBody> callback){
Call<ResponseBody> getProfilna = service.GetProfilna(UserId);
getProfilna.enqueue(callback);
}
}
This is my UserRespository class:
public class UserRepository {
private static volatile UserRepository instance;
private final ApiManager apiManager;
private final MutableLiveData<ResponseBody> responseBody = new MutableLiveData<>();
private UserRepository(ApiManager apiManager){
this.apiManager = apiManager;
}
public static UserRepository getInstance(ApiManager apiManager){
if(instance == null){
instance = new UserRepository(apiManager);
}
return instance;
}
public MutableLiveData<ResponseBody> GetProfilna(String UserId){
apiManager.GetProfilna(UserId, new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()){
responseBody.setValue(response.body());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
return responseBody;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
