Lilla Nagy Lilla Nagy - 19 days ago 6
Android Question

How can you change the content of a drawable image file in Android?

I have an image called graph.jpg in the drawable folder, and I want to fill it with a certain image every time I download it from a server. For this, I used Retrofit, and I already have the following interface:

@GET("/androidimage/")
Call<ResponseBody> getImage(
@Query("image") String command
);


And the following method in the activity:

private void getImage(WebService webService, String command){
Call<ResponseBody> call = webService.getImage(command);
call.enqueue(new Callback<ResponseBody>() {

@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
if (response.body() != null){
Bitmap bm = BitmapFactory.decodeStream(response.body().byteStream());
imageViewTouch.setImageBitmap(bm);
}
else {
imageViewTouch.setImageResource(R.drawable.graph);
}
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
System.out.println("LOG Error: " + t.getMessage());
}
});
}


I want my graph.jpg file to be changed to the most recent bm, converted to JPG. How can this be done?

Thanks to @Alex Klimashevsky's answer, I was able to do this using the Picasso library, and it's much simpler.

Answer

Use Picasso library for it.

This library has caching mechanism from the box.

Also you cannot replace images from /drawable folder.