Lets say, i am creating a news app and also fetching the images from server.
Which is best place to save those images? in Cache Directory or external directory? since those images might be 500kb or sometimes even 1 MB or less.. How to manage those images since those files are app specific and user can read news containing images in offline too..
Currently i am saving in internal cache directory by creating another folder inside cache directory. If i save it in SD Card then wont it create problem for the phone or tablet which don't have Memory Card?
Any Alternative best way?
Use this https://github.com/nostra13/Android-Universal-Image-Loader lib to loading images , it do a lot of work for you. To cache images you on sdcard just needed to properly set up ImageLoaderConfiguration and DisplayImageOptions fields.
Related
Scenario
I have made an application that displays a list of predefined products to the user. Each product has an image associated with it.
Initially to display the Image on the app, I had used ImageLoader which utilities the Image URL. But it takes up a lot of the users mobile-data.
To minimize the users mobile-data usages, I was planning to save the images on the users device when the app is installed and occasionally(once a month) when new items are added to the list.
Question
When the app is installed or if their are updates to certain image items is it better to download & save the images directly in the cache or as a hidden folder in the device storage ?
In my research I realized that
In the cache : Lesser Viewed Images/Objects are evicted from the cache.
(I have a check in my code to see if the image to displayed is still in the storage, if not it will intern lead to downloading the image again.which i don't want)
In the device storage: It may not be ethical to hide Folder and Images on the device.
(the reason why the Images and Folder is hidden is because i don't want the user to view the images in their Galley or delete the folder by mistake)
My Goal
To ask your opinion for the best solution to the above scenario.
Weather to save the images in the cache, device storage or some other method.
My Aim
How to minimize the users mobile-data usages.
If the images of products are not updated frequently than you can save the images in the memory.
You can use the private folder of your application which is not shared by other application and the images which you saved is not displayed in the gallery.
The folder is inside the data/data/yourPackageName
You can get the path of the folder by below line:-
getApplicationInfo().dataDir
ImageLoader is using the cache memory to temparary save the images into your device so if the image is present in the cache it will not download again instead of that it will take from cache.
It totally depend upon you, if you save the images on the phisical storage and if their are too much images and you are not managing well than it might be possible that your application take to much memory of the device.
Hope it will help you.
You can use Glide or Picasso 3rd party libraries available which will reduce load time and once it is load it will cache image and display cache for next time.
Below is code to use
Add below in your app.gradle
compile 'com.squareup.picasso:picasso:2.5.2'
Source to load an image from URL into Imageview
Picasso.get()
.load("your image url")
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.fit()
.into(imageView)
My Android app creates a folder on the users device's external storage on launch:
File images = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "rapical" + File.separator);
images.mkdirs();
I have around 16 image icons (.png) that I would like to place in this folder the first time the user opens the app.
What is the best practice to do this? Should I place them in the drawable folder initially and then copy them over to the newly created images folder? Not sure what approach to take!
There are two separate issues here:
What to do with images that the user adds to your app?
What to do with your starter images, for your default foods?
Using external storage for the user-added images has some implications:
Your app can survive that image no longer existing, since the user, or other apps, can delete that file at any point
Your user does not mind that the image will get picked up by other apps, such as photo galleries
Unless you specifically want these images to be user-manipulable outside of your app, I suggest that you use internal storage for the "re-sized, compressed and stored" user-supplied images. The original image might be on external storage (I assume that you are using ACTION_GET_CONTENT and/or ACTION_OPEN_DOCUMENT to get the image), but your modified copy would be private to your app.
I sincerely hope that you are using an image-loading library, like Picasso, for loading these images, since they will handle things like background threads and ListView/RecyclerView image recycling and stuff for you.
In that case, what you store in the SQLite database for your default foods needs to be something that the image-loading library can interpret, to bring in the image that you want.
In that case, I would suggest using assets/ to ship the images and file:///android_asset/... values in the database. file:///android_asset/ points to what amounts to assets/ in your project, so if you have assets/chicken_pad_thai.jpg in the project, Picasso (and any decent image-loading library) would be able to interpret file:///android_asset/chicken_pad_thai.jpg) and load the image.
This saves you from making duplicate copies of the images, saving the user disk space and time on first launch. It also means that if you replace the images in your app (e.g., you get a better photo representing chicken pad thai), the user will start seeing the updated image, without you having to do some extra work to realize that you shipped a new image and have to copy that image out to a file somewhere.
Now, suppose that you really do want the user-supplied images to be on external storage. In that case:
Probably rather than the directory that you chose, I would go with getExternalFilesDir() (a method on Context), as on API Level 19+, you do not need WRITE_EXTERNAL_STORAGE as a permission
If you want the user to be able to manipulate the images of the default foods, then copy those out to that location on first run
I'm using Universal Image loader to retrieve a lot of images from a server and showing it inside a listview.
Now when cache is full, library remove automatically some "old" images for free space to keep new on, so when i scroll "back" for see previous images (that was deleted from cache), library download it again.
This is a very big problem if library recover picture using a 3g connection.
What i want to do is:
-recover picture from link
-save pic in cache (library already do this)
-save pic into a folder on storage too
so when library need to show again an old picutre that is not still into chache, it could recover it from storage, and not from web.
It's possible to do it?
I think the batter way is you use UrlImageViewHelper you can download from https://github.com/koush/UrlImageViewHelper
it will handle automatically store the image in cash memory it's a better way to use this lib to load image.
Yes you Can First you have to store images in specific folder while doing activity for first time later you can put small piece of code to check that requested file is present on local storage folder ..... thats will solve your Problem
I am downloading a number of small images from the server. My question is: where do I store them? The images are icon size and I am expecting at the very most 100 of them. I want to use a HashMap to store them. But I am wondering if I should be storing them on the SD card. And if I decide to store them on the SD card: Do I have to "inflate" the bitmap each time a user calls the image (which is used on multiple activities) or would I be storing them in a data structure anyway at that point? To me the obvious answer is HashMap. But just in case I overlook something, does anyone know reasons for which way I should go?
Why not cache them on external storage or internal storage? This is actually how Android Gallery has done. You can find cache file at /sdcard/Android/data/gallery_package/cache/cache_file. In Gallery, you can access Picasa data which will be downloaded by gallery and cached to local disk. when drawing these data on screen, you can load them from cache directly.
I want to store images locally, So which is efficient way , storing image in data base or storing image in internal memory. I have no option of external memory.
Best wud be to pre bundle the images in your apk itself. Else there is no way to do that.Also if you can save images on the sd card, you can make the folder names start with "." (dot) so that they wont be visible in normal condition (will be visible only if there is option checked of "show hidden files" in your file browser of phone).And if you want that your images dont show up in gallery then just add an empty file to the folder named ".nomedia".This will make your images not to shpw up in Android Gallery.
This page shows the various options you have for data storage. If you just need to save images, I can't see why you would use a database. Also, your database would be located in Internal memory.
Images stored in private internal memory do not show up in the gallery as they are only accessible to the app that created them.