I created this method to take in a URL, retrieve the PNG file from that URL, compress it, and save it to the SD card. The method then gets that file and uses it as a sprite on the Live Wallpaper.
try
{
URL url = new URL(
"http://www.google.com/someimage.png");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
webBitmap = BitmapFactory.decodeStream(input);
f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/webbitmap.png");
// Save the bitmap to the sdcard.
String filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(filepath + "/"
+ "webbitmap.png");
webBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
if (f.exists())
{
BitmapTextureAtlas texture = new BitmapTextureAtlas(512,
1024, TextureOptions.BILINEAR);
FileBitmapTextureAtlasSource fileTextureSource = new FileBitmapTextureAtlasSource(
f);
TextureRegion textureRegion = TextureRegionFactory
.createFromSource(texture, fileTextureSource, 0, 0,
true);
this.mEngine.getTextureManager().loadTexture(texture);
webSprite = new Sprite(512, 1024, 320, 634,
textureRegion);
webSprite.setPosition(
mCamera.getCenterX()
- webSprite.getRotationCenterX(),
mCamera.getCenterY()
- webSprite.getRotationCenterY());
scene.attachChild(webSprite);
}
else
{
scene.attachChild(GTMessage);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
For the most part it is working great. However, occasionally when this method is run an IllegalArgumentException (Error loading bitmap) occurs on this line:
webBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
Here is the stack trace:
01-24 12:50:52.739: E/AndEngine(19411): Error loading: FileBitmapTextureAtlasSource(/mnt/sdcard/webbitmap.png)
01-24 12:50:52.739: E/AndEngine(19411): java.lang.IllegalArgumentException: FileBitmapTextureAtlasSource: FileBitmapTextureAtlasSource(/mnt/sdcard/webbitmap.png) returned a null Bitmap.
01-24 12:50:52.739: E/AndEngine(19411): at org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:159)
01-24 12:50:52.739: E/AndEngine(19411): at org.anddev.andengine.opengl.texture.Texture.loadToHardware(Texture.java:116)
01-24 12:50:52.739: E/AndEngine(19411): at org.anddev.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:146)
01-24 12:50:52.739: E/AndEngine(19411): at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:503)
01-24 12:50:52.739: E/AndEngine(19411): at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154)
01-24 12:50:52.739: E/AndEngine(19411): at com.*****.*****.GLThread.guardedRun(GLThread.java:236)
01-24 12:50:52.739: E/AndEngine(19411): at com.*****.*****.GLThread.run(GLThread.java:95)
Is there anyway to at least prevent this force close from occuring? Or if there was a way to fix it for good that would be ideal.
Thank you
//EDIT – My Solution
After researching where the exception is thrown, I found out it is being thrown in the BitmapTextureAtlas.java class. Specifically this line:
if(bitmap == null)
{
throw new IllegalArgumentException(bitmapTextureSource.getClass().getSimpleName() + ": " + bitmapTextureSource.toString() + " returned a null Bitmap.");
}
I found out that the bitmap was not actually null, it seemed to just be “corrupted”. This was due to the fact that the file already existed and it was in the process of overwriting it. This is how I fixed the issue:
f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/webbitmap.png");
if (f.exists())
{
f.delete();
}
I no longer receive the error.
You can use throws IllegalArgumentException in the class which has this snippet.
Look here for more details
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/IllegalArgumentException.html