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?
Related
I am completely new to Android studio, but I would really like to create a music player that plays downloaded songs on android. My problem is that if I have a lot of songs they use up a lot of space and my question is if there is a way to compress the files in a way that they use up less space, but still get unpacked fast enough to not cause big lags between one song and the other.
The idea behind this is that when a song is selected it gets unpacked, then gets played and when it finishes it gets packed again. I don't know if that's the best way to do it, and if you have a better idea please let me know.
This is not possible. MP3 files (mentioned in tag) are already effectively compressed. The only way to make it significantly smaller is to re-encode it with lower bitrate which would be lossy compression. It would seem that external storage/hosting and loading the necessary files on the go is the only way to save device storage. To have no lag, keep "one song in advance from the playlist".
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 am in the middle of writing an android app while teaching myself java all at the same time and my project is a bit cumbersome for a first time app. NOTE:
I am not a newbie to programming, I know several languages with the exception of java and writing code for mobile devices. With this all said and done, I don't want to have to force my users to have to update the app every time there is a new image.
Is there a way, I am assuming there is, to check on startup if all the images are up to date and if not download the new ones to the drawable resource folder for use in the app?
I have searched and found ways to remotely fetch items but nothing specifically for updating app images. For example: Splash Screen Image, or background image etc.
Thanks for your time and apologize if this does not conform to stackoverflow form of asking a question.
Shawn Mulligan
Based on my knowledges, there is no way to do why you're asking for. Resources are static contents and there no way to modify or update them programatically.
To achieve a kind of feature that you describe, it should be through a distant server, eventually with a local cache of your images on disc, associate to a download date.
But you won't be able to use them in the exact same way that you can use drawable resources.
Also depends of what you really mean by "if images are up to date".
i'm actually adding a music player in my android app. It will contains 8 albums, 12songs in each one. So i'm thinking about the best way to do this. Should i store the mp3 songs in the app, which will make there lecture faster and won't need access to internet. Or maybe it's too much heavy and calling mp3 url would be a better idea?
Thank you
There are a few things to consider when evaluating your options.
If you ship the songs together with the app, you will greatly increase it's size. Most probably you will go over the 50 Mb limit, and you will have to implement APK expansion files, which are downloaded when the app is first started. Implementing this is not very hard, but it can take some time.
If you implement a custom download for the songs, you can optionally provide better control over what is downloaded, optimizing bandwidth usage, download times, etc. Still, it would be more difficult to implement then the standard APK expansions, and you will have to deal with a lot of additional download logic - pausing / resuming, dealing with insufficient storage space, etc.
My advice is to go with an APK expansion, which is downloaded when the app is first started and will manage all the download complexity out of the box. If you need a more fine grained download - go custom.
Good luck.
I am developing an Android application where additional content is downloaded at user request. This includes images, sounds and classes. What I need is to load those resources and classes at runtime and use them. Is there a way I can do this?
Edit: I need to do this for Android 2.2 or higher.
Take a look at AsyncTask and Loaders API. This will provide you the facilities to download images, resources... in a background thread.
http://developer.android.com/reference/android/os/AsyncTask.html
http://developer.android.com/guide/components/loaders.html
I would use AsyncTask and cache them to the local memory, SD card or hard drive. You could start the AsyncTask on the load, cache the data to memory, then call it when it is needed. Here are a couple links that could help you with this:
AsyncTask:
http://www.peachpit.com/articles/article.aspx?p=1823692&seqNum=3
Caching:
http://developer.android.com/guide/topics/data/data-storage.html
Hope this helps!