Lilla Nagy Lilla Nagy - 1 year ago 67
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:

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>() {

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()){
if (response.body() != null){
Bitmap bm = BitmapFactory.decodeStream(response.body().byteStream());
else {

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 Source

Use Picasso library for it.

This library has caching mechanism from the box.

Also you cannot replace images from /drawable folder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download