I have a question. The project on the main screen I use 7 images in .png format size for 50 - 60 kb. At the same time when the application runs on the phone Monitor shows that the device uses 20 mb memory. How to upload pictures to use as little memory? Thanks for the answer.
I used the standard installation in the layout, the installation program
main_image.setImageDrawable(getResources(getDrawable(R.drawable.main).);
and glide
library(Glide.with(getApplicationContext()).load(R.drawable.main).placeholder(R.color.white).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(main_image);),
it is not bad but quite long loads images.
This question is similar to the post Images taking too much memory. The reason why for a small sized image you are getting large memory consumption is most likely due to usage of high pixelled images that use up more of your space when stored in Bitmaps. Infact I realised the answer for the high memory usage for small images just today when Dimezis pointed out my folly in just considering size and not looking into resolution of the image (link).
Now for the solution, I would recommend using Universal Image Loader, a image loading library that is pretty widely used. In it, there is an option to load the images in RGB_565 which can decrease your memory consumption by about half (have seen the results practically) and many more. Link to a more detailed answer.
And ofcourse I recommend you too to look into the
Related
I'm sort of new to managing memory in apps and have a question with regards to bitmaps.
I have many users that can each upload a profile image to be stored in firebase storage.
I'd like to be able to display these images as quickly as possible. The two options I see is
Loading them in the background at start up and storing the bitmaps, then referencing them later when they're needed, or
retrieving them when they're needed using Picasso or Glide and the urls (however this causes a small delay betweenAn when the page is loaded and the images are displayed, it also causes the need for more database calls).
By storing the bitmaps am I screwing myself over with regards to memory? Im testing with 10 made up users right now but if i have a 100 will the app be able to handle it?
Using Picasso or glide is the best way to load the images because they use cache as well, so there will be no unnecessary api calls. Also, they use highly optimised algorithms such as LRU, which can free the object/image which is not used for long time and clear those images from the local cache.
I'm a 17 year old trying to start developing some android games. I've used LibGDX once before and found it a pretty effective tool, so I'm using it again now.
The game I'm making is a choice based, interactive game where you make a choice and then the next scenario happens based on your choice, and it goes on and on until your character dies or you win. I'm expected to have around 200 scenarios by the time I'm done, and currently have around 160.
The problem I'm having is that each of these scenarios is basically a "card," with a picture, scenario description and 2 options below it. Each of these images is pretty big, and if I scale the card images down they start looking pixelated on the phone screen. I'm worried that in just images, my game will reach 100mb, and then with sound effects and everything else it might be like 200mb. This seems pretty inefficient and I don't want potential players to shy away from the game just because of it's size, if they don't have enough room on their phone...
Am I doing something wrong? I apologize for this inexperienced question, I'm really new to Android development.
That isn't too big for modern games but you will need to not include the assets in the apk and either download that when needed or use what Google already thought of for this problem.
https://developer.android.com/google/play/expansion-files.html
These are some steps that can help you reduce your apk size:
Use only specific drawable
Add only specific image size for each drawable directory for drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, etc. You can try removing a drawable directory that could potentially unused by the user, like drawable-mdpi and drawable-xhdpi, and use only the hi-res one like drawable-xxhdpi. Or, you can try using only drawable directory for all the images, so your app will only use one image for all devices type.
Resize your images
Compress your images
If your images are PNG files, You can compress the images without a noticeable change using pngquant. In fact, it can reduce your images sizes significantly (often as much as 70%) and preserves full alpha transparency. Or you can try using pngcrush (I'm rarely using this)
I am writing an android app, which needs to draw a huge amount of pictures aligned in a grid. The plan is to draw about 2000 pictures (building up a mosaic). Each of these pictures has a size of 1024x1024. Of course I ran there into heavy efficiency problems.
Right now I am using OpenGL ES 2.0 for drawing. I have one thread loading the images from resources into bitmap objects and passing them to a central pool. The rendering thread of OpenGL then takes an image from this pool, loads the image data from the bitmap to the GPU and draws it at the right cell of a built up grid.
This is horribly slow. I measured some times on this procedure using the android emulator. I got about 60ms for loading a resource into a bitmap object. Loading bitmap into GPU + drawing it takes about 15ms.
I don't have a clear idea on how to speed this up and do this with at least like 30 FPS. My ideas at this moment are to use an external library for loading bitmaps and maybe use the NDK to speed things up. Also I know about texture atlas and batching up my draws. I could implement this, but as my problems are with loading images from resources on runtime I didn't try it for now.
Maybe someone can give me an advice on how to do this efficiently.
Thank you everyone.
First of all, I just want to make it clear, since I think your title is a but misleading - your real problem is not drawing but loading, according to your description. You did not really say if you have a drawing problem (you mentioned the fps for loading and drawing).
You did not mention all the requirements, for example - how soon do you need to draw all of them? Maybe you can, for example, load the first X as part of the app loading, so when the app loads the textures are already created and can be drawn, and in the meantime in the dedicated thread load another X, and so on...
Another approach would be to have low resolution images and high resolution images, load the low resolution first and replace later (on-demand, for example). I'm not sure if this is reasonable memory-wise for 2000 images, but you can check that.
Also, I would suggest taking a look at Rajawali - it is a nice framework, with good examples including for multiple textures (there's also examples app in Google Play). Perhaps it could help.
So I've tried going about animating a button through the example given in this example: Android Developers
and have made a huge (95 pictures) anim-list with Pngs. When trying to run the app and pressing the button, I promptly receive an error about memory not being sufficient. The file sizes are about 170k (12mb in total) . Is this too much for an animation? I realize the serious lack of regard for eventual users' storage space with this approach, but it's mostly for my own use anyway and won't matter.
The images are pretty big and need to be downscaled by the graphics engine however, and this might have to do with it?
Or is 95 pictures too much ? I can make the pictures smaller, right now they're at a whopping 4K resolution, which in hindsight is pretty dumb.
However, I can compress the pictures and make them smaller from the beginning anyway so that's not a problem. I just need to know how to fix this issue, if there's a treshold for how much can be loaded into RAM at a specific time so i can match the image sizes or if there's a way to override the default RAM settings or overcome this?
Don't look at the file size. Image has to be uncompressed so Android can use it. The image size is more or less equal to width*height*4. 4K image takes ~35MB of memory.
Modern devices can allocate 64MB of memory per app. It means that you can load only one such image. Maybe more if your phone has higher heap limit.
AnimationDrawable is for small animations (like 100x100px). If you wish to play such a large animation, you should encode it to mp4. If you can make the images smaller or make the animation procedural - go for it.
To override the memory limit use 'largeHeap' flag or change the setting in the ROM configuration (you have to flash your device with a custom ROM).
I have two libraries one is MuPDF to view PDF files in android and second is pageCurl to implement the page curl effect, but it works for bitmaps. Now, I have no idea how can i integrate both of these to get page curl effect in PDF files, opened by MuPDF. If any one has already implemented this or knows about some link then please help me.
Thanks
As far as I know there's no simple way to accomplish this.
I made a real world app for a wide spread national newspaper: the only way seems to work with bitmaps. They used server side pdf for iOS devices and jpg bitmaps for the Android counterpart.
Keep in mind that working with large bitmaps is a pain in android. That's the reason why pdf viewers renders them in chunk: if you scroll too much, in zooming mode, the high resolution chunks already loaded in memory keep being garbage collected and lost. Test it with whatever pdf implementation (even muPDF) you choose.
2 bitmaps at once (front page and back page, for example) with resolutions of say about 1200x800 eat up the whole app heap memory causing the infamous unrecoverable OutOfMemory exception. Every device has a fixed (and different) amount of heap memory to use for the objects in memory and suffer max allocation limitations too.
My suggestion is:
save your time and change implementation to prerendered images from pdf (server side the best if possible, or prerender directly on device) and manage low memory limited device with different resolution ones.
I'm an Android enthusiast developer but I'm waiting ages for some all-pdf components that could accomplish what iOS devices already do flawlessly. Good luck.