Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:48:33+00:00 2026-06-07T00:48:33+00:00

I’m building a relatively basic news-reader app that involves displaying news in a custom

  • 0

I’m building a relatively basic news-reader app that involves displaying news in a custom listview (Image + Title + Short Description per list element).

My question is How can I store the images I download from the server and then attach them to the listview? The images will be relatively small, 200 X 200 usually, in .jpeg format.

It’s not so much a question of how as much as “how to do it efficiently”, as I’m already noticing lag in lower-end phones when using the default “ic_launcher” icon instead of bitmaps.

Would it be faster to store them as files or into the news database along with other news data when the app starts and syncs up the news or cache them…?

How should I go about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:48:35+00:00Added an answer on June 7, 2026 at 12:48 am

    better you can do it’s use SoftReference via an ImageManager class.

    In you ListAdpater getView() method call the displayImage() method of ImageManager.

    ImageManager Coding Exemple :

    public class ImageManagerExemple {
    
    private static final String LOG_TAG = "ImageManager";
    
    private static ImageManagerExemple instance = null;
    
    public static ImageManagerExemple getInstance(Context context) {
        if (instance == null) {
            instance = new ImageManagerExemple(context);
        } 
        return instance;        
    }   
    
    private HashMap<String, SoftReference<Bitmap>> imageMap = new HashMap<String, SoftReference<Bitmap>>();
    
    private Context context;
    private File cacheDir;
    
    private ImageManagerExemple(Context context) {
        this.context = context;
        // Find the dir to save cached images
        String sdState = android.os.Environment.getExternalStorageState();
        if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
            File sdDir = android.os.Environment.getExternalStorageDirectory();      
            cacheDir = new File(sdDir,"data/yourappname");
        } else {
            cacheDir = context.getCacheDir();
        }
        if(!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
    }
    
    
    /**
     * Display web Image loading thread
     * @param imageUrl picture web url
     * @param imageView target
     * @param imageWaitRef picture during loading
     */
    public void displayImage(String imageUrl, ImageView imageView, Integer imageWaitRef) {
        String imageKey = imageUrl;     
        imageView.setTag(imageKey);
        if(imageMap.containsKey(imageKey) && imageMap.get(imageKey).get() != null) {
            Bitmap bmp = imageMap.get(imageKey).get();
            imageView.setImageBitmap(bmp);
        } else {
            queueImage(imageUrl, imageView);
            if(imageWaitRef != null)
                imageView.setImageResource(imageWaitRef);
        }
    }
    
    private void queueImage(String url, ImageView imageView) {
        ImageRef imgRef=new ImageRef(url, imageView);
        // Start thread
        Thread imageLoaderThread = new Thread(new ImageQueueManager(imgRef));
        // Make background thread low priority, to avoid affecting UI performance
        imageLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
        imageLoaderThread.start();
    }
    
    private Bitmap getBitmap(String url) {
        String filename = String.valueOf(url.hashCode());
        File f = new File(cacheDir, filename);
        try {
            // Is the bitmap in our cache?
            Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
            if(bitmap != null) return bitmap;
            // Nope, have to download it
            bitmap = ImageServerUtils.pictureUrlToBitmap(url);
            // save bitmap to cache for later
            writeFile(bitmap, f);
            return bitmap;
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(LOG_TAG, ""+ex.getLocalizedMessage());
            return null;
        }  catch (OutOfMemoryError e) {
            Log.e(LOG_TAG, "OutOfMemoryError : "+e.getLocalizedMessage());
            e.printStackTrace();
            return null;
        }
    }
    
    private void writeFile(Bitmap bmp, File f) {
        if (bmp != null && f != null) {
            FileOutputStream out = null;
    
            try {
                out = new FileOutputStream(f);
                //bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
                bmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally { 
                try { if (out != null ) out.close(); }
                catch(Exception ex) {} 
            }
        }
    }
    
    
    private class ImageRef {
        public String imageUrl;
        public ImageView imageView;
    
        public ImageRef(String imageUrl, ImageView i) {
            this.imageUrl=imageUrl;
            this.imageView=i;
        }
    }
    
    private class ImageQueueManager implements Runnable {
        private ImageRef imageRef;
        public ImageQueueManager(ImageRef imageRef) {
            super();
            this.imageRef = imageRef;
        }
        @Override
        public void run() {
            ImageRef imageToLoad = this.imageRef;
            if (imageToLoad != null) {
                Bitmap bmp = getBitmap(imageToLoad.imageUrl);
                String imageKey = imageToLoad.imageUrl;
                imageMap.put(imageKey, new SoftReference<Bitmap>(bmp));
                Object tag = imageToLoad.imageView.getTag();
    
                // Make sure we have the right view - thread safety defender
                if (tag != null && ((String)tag).equals(imageKey)) {
                    BitmapDisplayer bmpDisplayer = new BitmapDisplayer(bmp, imageToLoad.imageView);                         
                    Activity a = (Activity)imageToLoad.imageView.getContext();                          
                    a.runOnUiThread(bmpDisplayer);
                } 
            } 
        }
    }
    
    //Used to display bitmap in the UI thread
    private class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        ImageView imageView;
    
        public BitmapDisplayer(Bitmap b, ImageView i) {
            bitmap=b;
            imageView=i;
        }
        @Override
        public void run() {
            if(bitmap != null) {
                imageView.setImageBitmap(bitmap);
            } 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.