开发者

android - where to store the downloaded images

开发者 https://www.devze.com 2023-04-05 19:44 出处:网络
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.S

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 th开发者_StackOverflow中文版e 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


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.


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;

     }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号