My app shows images to user one after one. The images are downloaded from server. As I do not want the user to wait for the images to be downloaded I cached those in a local file system (say new 50 images) .
Implementation:
from onCreate method start a AsyncTask that will keep in downloading images in background.
Is AsyncTask best for this use case?I do not want to use a Service as I do not want the download to happen continuously
Is AsyncTask best for this purpose?
I would suggest using an existing library, such as Picasso (by Square) or Volley (by Google).
Picasso is especially easy to implement and will work excellently for your purpose, and is as easy as:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
You won't have to concern yourself with AsyncTasks or AsyncTaskLoaders as the library takes all of this out of your hands, caching both images in memory and on the disk.
If you insist on building your own solution, I suggest looking around for related questions as there are many things to bear in mind:
AsyncTasks may be ill fitted as they are directly coupled with a single activity; you'll need a Loader or a mechanism similar to Loaders to deliver results to whichever activity wants an image.
On older devices, bitmaps reside in a special region of memory that isn't managed by the GC; you'll have to dispose of bitmaps manually when you're done with them.
Bitmaps use a significant amount of memory and need to be carefully managed to avoid OOMEs, such as downsampling them when loading and storing in memory inside an LruCache.
Beware of managing the size of the image cache directory when writing images to disk.
Yes, you can use Asynctask for this purpose. And using one of the apis of Asynctask "onProgressUpdate(), you can in parallel update the UI as your images are getting downloaded.
you can use universalImageLoader library for downloading images in an efficient way.
see this how to use universal Image loader for downloading images
One more solution for your requirement is Volley Library
Related
Okay so i have pictures on server. I would like to display them in my app as a part of a ListView row. I've already made that and now I have a question regarding the dimensions of the pictures. Since its a bad idea to save my image at 48dp sizes on server (36x36, 48x48, 72x72, 96x96 ..) and then download specific one for specific DPI device. Is it possible to only save a 96x96 and then set it to an ImageView. ImageView width and width = 48dp, and scaleType= fitCenter. Are there any better ways? please help me
You're right that you need only one image size on a remote server like 96px and then in Android app just fit it to your needs like 48px.
The easiest way to implement it would be using Picasso library.
Of course, there is a Glide, with many extra features, (so it might look at the beginnig a bit complicated for beginners), but for the most of uses Picasso is a enough solution.
You can also try a new library developed by Facebook, called Fresco,if you care also about memory using but I highly recommend you a Picasso.
Here you would find nice tutorial for this library:
https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit
If you would find more information about these three libraries visit this site
Fragmented Podcast - 005 – Image libraries for Android
You can use Glide or Picasso for that. It's very easy to implement.
Here is a guide that explains a little as the two libraries and do what you ask.
If not what is a good way to cache images locally?
-- Note: I am coding for Android.
Picasso.
Many common pitfalls of image loading on Android are handled
automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.
When you want to send the image to your cloud, you could use ParseFile in conjunction with Picasso, get the bytes from the image and save() or saveInBackground().
While working on my Android application, I recently check a task manager to see how memory my application is racking up.
Right now my application has two ad banners being executed by AdMob services and then an interface fragment for a Google Map. Aside from all that I then have a menu in which I utilize a container to which I add views to it in Java code. I prefer this over a list-view as it is more light weight and easier for me to structure what I want to do with the views (and I can use Androids native animation system when adding/removing the views).
The views being added to the container are being added by a loop structure based on conditions. The views in their interface structure also have a small image-view utilizing a small local drawable.
All of this is somehow adding up to my application 44.8 MB in memory which seems abnormally high from previous development numbers. This is also the first time I decided to use the container to add views through a loop rather then the adapter with list-view method.
Can anyone tell right off hand why my application is taking up so much memory right off? This is my first time utilizing all these components: AdMob Banners, Google Maps, and Containers with added Views.
My LogCat shows no warnings of doing anything inefficiently. Just curious to see if I am doing anything wrong. If anyone has any ideas I am open to provide any source code.
I think 44.8Mb is pretty normal for a modern Android application. My bet is on Google Maps since it's very resource-consuming library (both CPU-wise and memory-wise). Anyway, you can try to sequentially remove each of the component out of app and see how it affects memory footprint.
As Andrey have already said, 44.8Mb is not too much for an Android application. But you can use DDMS to track allocations and heap updates to see exactly is taking up this memory. See this: https://developer.android.com/tools/debugging/debugging-memory.html
Besides, I would recommend using a ListView with adapter because items in ListView can be recycled, which can improve performance considerablly (See this tutorial).
I want to build an app which will allow to view online video and to cache it.
But I need help with this: when user hits the magic "Play" button I want to start downloading video on external storage and to play it at the same time. How do I manage the whole process? What callbacks of MediaPlayer should I use? Any hints?
Looking my projects:
https://github.com/master255/ImmortalPlayer One thread to read, send and save to cache data. Most simplest way and most fastest work. Complex logic - best way!
https://github.com/master255/VideoViewCache Simple Videoview with cache. Two threads for play and save data. Bad logic, but if you need then use this.
I'm working on an android game, and the concept is quite sound, but i'm constantly frustrated by outofmemory errors and finding myself spending the majority of my time attempting to avoid them.
As of now, all animation is done in a canvas using png image resources. I'm guessing that some of the bigger apps don't quite use this method since much more graphically impressive games than mine exist and run much better.
Where i'm getting the problems is when I first load the image resources and assign them to variable handles. I load all the images from the very beginning of the app starting in a constructor, and I often hit the outofmemory error at this point usually on devices running android 2.1 more so.
So my question is how can I avoid this or change the way i'm doing the animation to be easier on memory.
Re-scaling helps temporarily, but is unacceptable as a long term solution, I want the images to look good. And like I said, other apps look good and have much more animation so i'm sure it's possible to do this.
The other option I've been suggested is to more closely control the object lifecycles to only allocate images when I need them. This doesn't seem to be an ideal solution either because do to the setup of my app, most of the images need to be drawn most of the time, or at least may be called at any time.
My last idea is to possibly add in some code that de-allocates the memory when an image is not immediately being used. Again this doesn't seem ideal because in circumstances when for instance there is a single enemy on the canvas, every time his frame changes he would need to re-allocate an image for it, killing performance.
Anyone have any general ideas I should look into? I know i'm missing something big. Can I manually make the available memory larger? Is there a format of image with a smaller memory footprint.
I'm trying to get a better knowledge of the basics of whats at work here.
My entire drawable folder is only 864kb, That seems ridiculous to me. What is happening that makes 864kb worth of images exceed the memory limits? I'd like to know specifically where the problem is occurring. I need a solution, not a workaround.
Edit: I've also noticed that OpenGL seems to be the alternative to canvas I've been using. I've never used OpenGL... is this what i'm looking for? and if yes, how difficult would it be to switch from simply drawing to canvas to the equivalent in OpenGL?