Storing bitmaps in android studio vs fetching with url - java

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.

Related

Using pictures in the project

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

Should I store a large amount of pictures in Android studio?

I am building an app in Android Studio and I have a problem. The basic idea of the app contains one Relative_layout with two buttons inside.Every time I press one button it sets the background of the layout to a Random image I have stored in the "res/drawable folder". The thing is that I am going to have a very big amount(larger than 10.000) of pictures(with high resolution) to be stored in that folder. Will this have a big effect on the APK size of the app? And if it does, is there any other more efficient way to store(or connect) a large amount of pictures with an android app?
You have 3 options:
Store the images as local resources, with very high res: Your application size will be huge
Stroe the images as local resources, but with less resolution: Your application size will be smaller
Store all the images on a web server, and retrieve them when you need it(Using picasso is super easy): Your application size will be very small.
You decide.
if the number of images is large, it is better to store at server and load dynamically when required, use Glide(fast and efficient open source media management and image loading framework), you can load image and display with a single line of code.

Android best practice - download pictures

How would you approach this problem:
My app should download different packages of pictures (containing up to 300 pngs, each about 20 kb) and store the pictures on the phone, so i can display them.
I want to upload the pictures somewhere online, so I can change them every time and the user can enjoy the newest pictures. (I upload the pictures not with the app)
I read that storing them in a sqlite db isn't the best option.
At the moment I am storing the pictures in the app, but then I don't know how I can upload and replace pictures on all apps immediately without the need of updating the whole app.
I don't need code or stuff, so don't waste your precious time on that, just some general hints where and how you would store the pictures online, and how android can download the pictures easily.
Take a look at the Glide or Picasso libraries. Those are super easy to use for thread-safe downloading of images.
Personally, I just fetch/store the images on imgur.
If you want to upload a dedicated databse, you'll have to set one up. Some common ones are Amazon, Google, etc. There are tons.
Have a look at this answer. In this answer Picasso library is used to handle image download. Picasso gets rid of a lot of coding and testing to handle image download.
In a project that I am working on, we use Amazon S3 to store our pictures, it's very reliable and is one of the goto solutions right now.
From what I heard Snapchat and some other big firms use S3 to store their picture!
It's also very cheap, plus I believe they have free hosting to a certain degree.
This is their API guide for android.
We use a service called File Picker to handle upload and download from amazonS3, it reduces a lot of work, but I don't think it's a free service.
You can use Picasso for downloading images from network in Android. For storing images Amazon S3 or Google cloud storage can be your options.
Not sure if downloading packages is better than downloading individual pictures (archiving won't save you much space).
As for your question, you can make some kind of API you will query from your app, even a flat file hosted somewhere with changing content would work. Your app could check it periodically for the new address to download pictures from (assuming it will change).
another way is using push messages - sending out a push through GCM that your apps will receive that will notify them about new content available. It would even work when the app is closed.

Best way to load assets in android java

I have one android activity with a lot of calculations and everything is ok, but in the final version of my project I need to load a lot of bitmaps and sounds in the same activity and it gets a lot slower than it should be.. Sometimes the activity chrashes itself.
So, is there any advice how should I load all the assets? Should I declare them in separate class, or what?
Basically you only load those assets when you need them and unload them if you do not need them. (Lazy Loading)
Because mobile has limited memory and I am imagining your files are really big you must be careful on how you will manage those files.
For image I recommend this link and try to optimize your image as good as possible
For sounds kindly look at this class and this discussion about loading sound files
OutOfMemoryError when loading large amount of data in Android
Android SoundPool – How to check if sound file is loaded?

How to implement Page Curl in MuPDF

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.

Categories

Resources