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

  • SEARCH
  • Home
  • 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 7544109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:36:08+00:00 2026-05-30T08:36:08+00:00

I am making an android app in which I am trying to lazyload and

  • 0

I am making an android app in which I am trying to lazyload and cache the images in a listview. I got the code from here. The example from this Github project works perfectly fine for me, but when i try to use the same example in my app it dosnt work. This is my ImageManager class.

public class ImageManager {

    private HashMap<String, SoftReference<Bitmap>> imageMap = new HashMap<String, SoftReference<Bitmap>>();

    private File cacheDir;
    private ImageQueue imageQueue = new ImageQueue();
    private Thread imageLoaderThread = new Thread(new ImageQueueManager());

    public ImageManager(Context context) {
        // Make background thread low priority, to avoid affecting UI
        // performance
        imageLoaderThread.setPriority(Thread.NORM_PRIORITY - 1);

        // 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/floapp");
            System.out
                    .println("coming here in mounted media and created folder floapp");
        } else
            cacheDir = context.getCacheDir();

        if (!cacheDir.exists())
            cacheDir.mkdirs();
    }

    public void displayImage(String url, Activity activity, ImageView imageView) {
        if (imageMap.containsKey(url)) {

            imageView.setImageBitmap(imageMap.get(url).get());
        } else {
            queueImage(url, activity, imageView);
            imageView.setImageResource(R.drawable.ic_launcher);
        }
    }

    private void queueImage(String url, Activity activity, ImageView imageView) {
        // This ImageView might have been used for other images, so we clear
        // the queue of old tasks before starting.
        imageQueue.Clean(imageView);
        ImageRef p = new ImageRef(url, imageView);
        synchronized (imageQueue.imageRefs) {
            imageQueue.imageRefs.push(p);
            imageQueue.imageRefs.notifyAll();
        }

        // Start thread if it's not started yet
        if (imageLoaderThread.getState() == Thread.State.NEW)
            imageLoaderThread.start();
    }

    private Bitmap getBitmap(String url) {
        System.out.println("coming in getbitmap");
        String filename = String.valueOf(url.hashCode());
        File f = new File(cacheDir, filename);
        System.out.println("cachedir=" + cacheDir.getPath());
        System.out.println("filename=" + filename);

        // Is the bitmap in our cache?
        Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
        System.out.println("bitmap after decoding the file thingy file path="
                + f.getPath());
        if (bitmap != null)
            return bitmap;

        // Nope, have to download it
        try {
            bitmap = BitmapFactory.decodeStream(new URL(url).openConnection()
                    .getInputStream());
            // save bitmap to cache for later
            writeFile(bitmap, f);
            return bitmap;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private void writeFile(Bitmap bmp, File f) {
        FileOutputStream out = null;

        try {
            out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (Exception ex) {
            }
        }
    }

    /** Classes **/

    private class ImageRef {
        public String url;
        public ImageView imageView;

        public ImageRef(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }

    // stores list of images to download
    private class ImageQueue {
        private Stack<ImageRef> imageRefs = new Stack<ImageRef>();

        // removes all instances of this ImageView
        public void Clean(ImageView view) {

            for (int i = 0; i < imageRefs.size();) {
                if (imageRefs.get(i).imageView == view)
                    imageRefs.remove(i);
                else
                    ++i;
            }
        }
    }

    private class ImageQueueManager implements Runnable {
        // @Override
        public void run() {
            try {
                while (true) {
                    // Thread waits until there are images in the
                    // queue to be retrieved
                    if (imageQueue.imageRefs.size() == 0) {
                        synchronized (imageQueue.imageRefs) {
                            imageQueue.imageRefs.wait();
                        }
                    }

                    // When we have images to be loaded
                    if (imageQueue.imageRefs.size() != 0) {
                        ImageRef imageToLoad;

                        synchronized (imageQueue.imageRefs) {
                            imageToLoad = imageQueue.imageRefs.pop();
                        }

                        Bitmap bmp = getBitmap(imageToLoad.url);
                        imageMap.put(imageToLoad.url,
                                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(imageToLoad.url)) {
                            BitmapDisplayer bmpDisplayer = new BitmapDisplayer(
                                    bmp, imageToLoad.imageView);

                            Activity a = (Activity) imageToLoad.imageView
                                    .getContext();

                            a.runOnUiThread(bmpDisplayer);
                        }
                    }

                    if (Thread.interrupted())
                        break;
                }
            } catch (InterruptedException e) {
            }
        }
    }

    // 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;
        }

        public void run() {
            if (bitmap != null)
                imageView.setImageBitmap(bitmap);
            else {
                imageView.setImageResource(R.drawable.ic_launcher);
            }
        }
    }
}

I get FileNotFoundException, but the files are being created in the folder on my sdcard. can anyone please help me??
-Thanks in advance

UPDATE:
I guess it has something to do with the url. I got a hint from here. My urls are like these “http://graph.facebook.com/1519317701/picture“

  • 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-05-30T08:36:10+00:00Added an answer on May 30, 2026 at 8:36 am

    I would suggest you to try this example for once:

    1. Lazy list with images by Fedor
    2. Universal Image loader for ListView, Gallery, GridView.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making an OpenGLES Android app using Android NDK, expanding from android's gljni example,
I am currently making an app which works with images. I need to implement
I am making an android app in which i have to show Hash Tag
I am making an android app in which I need to make different tables
I am making an android app in which i want to make a dynamic
I am making an Android app which requires a main view with similar functionality
I am making an android app in which radio buttons are to be created
I hope I can explain this properly. I'm making an android app that, when
We are in the making of an Android app, a quiz game, which we
I'm making an Android app where the user can download files from a FTP-server.

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.