Saturday, November 29, 2014

Load a Bitmap efficiently and set it as a wallpaper for Android devices

In this blogpost I will open the parts of the source-code of my discontinued app called DroidPapers, that I considered as code making my app different from other apps. In the past I released other code to the general public, that were also a part of DroidPapers.

The topic is about Bitmaps and how to efficiently load them into your device's memory and, if needed, set them as a wallpaper.

The code is aimed at Android and written in Java and you need API level 14 (ICS) or higher. Perhaps with some tweaking it might work for other Java-based platforms and Android versions. But that is up to you.

Some requirements I had while making this code:
  • A Bitmap of any size should be loadable on any device, both low-end as high-end.
  • Make as less Bitmaps in memory as possible.
  • Free Bitmaps in memory as soon it is possible.
  • Always check if Bitmaps, and other variables, actually exists in memory before processing.
There are most likely even better ways to process Bitmaps. The code is based on examples found on the Internet and the Android SDK website. The experience I had with this code that error reports about memory hogging were gone.

The code is explained in the code itself. I made two files with one containing the "library" and the other containing the process of the Bitmap.

LibraryWallpaperBitmaps.java

ProcessBitmap.java

Thanks for reading my blog and if you have any questions, feel free to contact me or respond in the comments below.

Sources


Copyright (c) 2015 Joram Teusink

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Share:  

0 comments: