我创建了这个方法来接收一个 URL,从那个 URL 检索 PNG 文件,压缩它,然后将它保存到 SD 卡。然后该方法获取该文件并将其用作动态壁纸上的精灵。
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();
}
在大多数情况下,它工作得很好。但是,有时在运行此方法时,此行会出现 IllegalArgumentException(加载位图时出错):
webBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
这是堆栈跟踪:
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)
无论如何至少可以防止这种力量关闭发生?或者,如果有办法永久修复它,那将是理想的。
谢谢
//编辑 - 我的解决方案
在研究了引发异常的位置后,我发现它是在 BitmapTextureAtlas.java 类中引发的。特别是这一行:
if(bitmap == null)
{
throw new IllegalArgumentException(bitmapTextureSource.getClass().getSimpleName() + ": " + bitmapTextureSource.toString() + " returned a null Bitmap.");
}
我发现位图实际上并不是空的,它似乎只是被“损坏”了。这是因为该文件已经存在并且正在覆盖它。这就是我解决问题的方法:
f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/webbitmap.png");
if (f.exists())
{
f.delete();
}
我不再收到错误。