My app uses images from the web to set wallpapers. However, when I try to use decodeStream
using the BitmapFactory, some users on slower phones get an OutOfMemory exception. This image is never displayed on the app, it is just loaded, and then set as a wallpaper. I've searched online, but I've been unable to find any resources which help solve the problem.
Code:
public void setWallpaperMethod() {
InputStream in = null;
try {
in = new java.net.URL(urldisplay).openStream();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final Bitmap bitmap = BitmapFactory.decodeStream(in);
myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(MainActivity.this,
"Teamwork! Wallpaper set.", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(
MainActivity.this,
"Damnit, clickers! Wallpaper wasn't set. Try killing and restarting the app?",
Toast.LENGTH_LONG).show();
}
}
});
}
The variable urldisplay
is a string, equal to a URL (e.g., an imgur image). Also, the method is run in a thread, so there's no risk of the UI thread locking up.
So does anyone know how to solve this OutOfMemory Exception? All help appreciated.