1

In my application, i have some url's using these url's i am downloading the images from web and use these images in my app. Where to store these images? because my app have lot of images are there.Suggest the best way i.e,that way have no chance to get the "out of memory exception".please can anybody help me.

I am using the following way is it good or bad. can anybody suggest me.

  1. Using the url's i am downloading the images from the web and at the download time i am creating the directory for these images in the sd card. Save these images with url names in the sd card.if the image name is exist in the directory then that image will be fetching from the sd card. otherwise it will be downloaded from the web. is it good?

thanks

naresh
  • 10,332
  • 25
  • 81
  • 124

3 Answers3

5

As per Saving cache files from the Android developer, use Context.getExternalCacheDir() (API level 8) or Environment.getExternalStorageDirectory() + /Android/data/<package_name>/cache/ (API level 7 or lower) as the location to store cache files.

If you have different caches, add a subdirectory for each.

Both of the above approaches are in your app's "private" data space and will be removed along with your app -- that's a good thing, you don't want to use space on a device when your app is gone.

Also, don't use the URL direclty as file name, create a digest/hash of it and use that, like the iosched app does. This spares you from escaping any special characters in the URL yourself.

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
  • You don't have to do anything yourself, when you store files in `Context.getExternalCacheDir()` or `Environment.getExternalStorageDirectory() + /Android/data//cache/` --- when the user uninstalls your app (or uses the "clear data" button in the system settings), the Android system will *automatically* remove your files. – Philipp Reichart Sep 16 '11 at 07:45
  • ya but in my app each screen have 15 images and scrollable. At the time of scrolling i am fetching the images from sdcard if those images are already downloaded and save in the ad card. But here async thread is used because of this u r not getting any dead lock cases. – naresh Sep 16 '11 at 07:54
  • Yes, doing network IO ouside the main/UI thread is a Good Thing(tm). If you need more help, please edit your question or open a new one, I'll have a look at it -- comments aren't for discussion :) – Philipp Reichart Sep 16 '11 at 07:59
  • How to integrate my code with ur code. code for downloading the images from web and sd card location. please refer this link http://stackoverflow.com/questions/7360166/android-how-to-copy-the-image-to-folder-in-sdcard – naresh Sep 16 '11 at 08:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3513/discussion-between-philipp-reichart-and-naresh) – Philipp Reichart Sep 16 '11 at 08:19
  • App is working fine but i got the following exception.java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.ibkr.elgifto/cache/bitmap_958b72108b52c2f96eafe8061f85f53ba015165e.tmp (No such file or directory). Related code is "FileOutputStream fo = new FileOutputStream(file); " – naresh Sep 16 '11 at 11:07
  • Add this *before* creating the `FileOutputStream`: `file.getParentFile().mkdirs(); file.createNewFile();` – Philipp Reichart Sep 16 '11 at 11:14
  • i am using this images are not visible. otherwise it's visible – naresh Sep 16 '11 at 11:25
  • Please open a new question for that, your caching problem is solved and this has most likely nothing to do with it. – Philipp Reichart Sep 16 '11 at 11:28
  • Please refer this link http://stackoverflow.com/questions/7444180/android-how-to-handle-file-not-found-exception – naresh Sep 16 '11 at 11:44
  • But still my app is working fine. Is any problem with that exception. Here where these images are stored. Because here u r getting the file not found exception. – naresh Sep 16 '11 at 11:50
1

Here's the code I use in Application object to create a external cache dir

@SuppressLint("NewApi")
        private File setupExternalCacheDir() {
                    File extCacheDir;

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                        extCacheDir = getExternalCacheDir();
                    } else {
                        extCacheDir = new File(Environment.getExternalStorageDirectory(), "/Android/data/" + getPackageName() + "/cache/");
                        extCacheDir.mkdirs();
                    }

         return extCacheDir;

     }
scottyab
  • 23,621
  • 16
  • 94
  • 105
0

If the images are too large, and are supposed to permanently stay on the device (of course unless the user wants to delete them), this method is the best.
However, if the images are small, and are not necessarily to stay on the device, I'd suggest to store them in the application cache, and clear the cache once the user exits the application. You can even try to push the images in an application specific database.

SamSPICA
  • 1,366
  • 15
  • 31