ibaralf
ibaralf

Reputation: 12528

Android ImageView - how to update displayed image using a downloaded image

I am actually wondering if this is possible: Basically I want to be able to give the user the ability to personalize the look of buttons. Right now I use ImageViews with icons and have the onClickListener() do something. If the user wants a different image for the button, I have an API that allows user to download a new image.

1) Activity uses ImageView, and sets image displayed using setImageResource

ImageView imgView = ...findViewById(R.id.imageView1)
String imgName = sqlData.getImageName();
int resId = context.getResources().getIdentifier(imgName, "drawable", 
      context.getPackageName());
imgView.setImageResource(resId);

2) User downloads a new image and saves it on his phone.

3) New image name is saved in sqlite

sqlData.setImageName("new_image.png");

4) How to display new image again using the setImageResource

Upvotes: 0

Views: 49

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 6179

Add Universal Image Loader library in your project and use this method:

ImageLoader.getInstance().displayImage(image_url, imageViewPic, options, new ImageLoadingListener() {

        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {
            // TODO Auto-generated method stub
        }
    });

in onLoadingComplete() method, set image again to your imageView option value is:

DisplayImageOptions options;
options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.calenderback)
    .showImageForEmptyUri(R.drawable.ic_empty)
    .showImageOnFail(R.drawable.ic_error)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .considerExifParams(true)
    .bitmapConfig(Bitmap.Config.RGB_565)
    .build();

Upvotes: 0

Related Questions