在Android/AndEngine中保存位图时,如何防止出现非法argumentException?
我创建这个方法是为了接收一个URL,从该URL检索PNG文件,压缩它,并将其保存到SD卡。然后该方法获取该文件并将其用作实时墙纸上的精灵在Android/AndEngine中保存位图时,如何防止出现非法argumentException?,android,bitmap,andengine,live-wallpaper,Android,Bitmap,Andengine,Live Wallpaper,我创建这个方法是为了接收一个URL,从该URL检索PNG文件,压缩它,并将其保存到SD卡。然后该方法获取该文件并将其用作实时墙纸上的精灵 try { URL url = new URL( "http://www.google.com/someimage.png"); HttpURLConnection connection = (HttpURLConnection) url
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(加载位图时出错):
if(bitmap == null)
{
throw new IllegalArgumentException(bitmapTextureSource.getClass().getSimpleName() + ": " + bitmapTextureSource.toString() + " returned a null Bitmap.");
}
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();
}
我不再收到错误。您可以在包含此代码段的类中使用Trows-IllegalArgumentException 查看此处了解更多详细信息
我想真正的问题是内存管理。使用加载位图时 ,您可以出于不同的原因获取空值。如果保证图像的格式正确,则如果图像超出可用内存,则可能会获得空值
您可以查看logcat,看看它是格式不正确还是内存问题 是的,它总是保证是一个PNG文件,并且URL总是可以访问的。我还检查了内存,那里的一切似乎都正常。不过,我还是想验证一下spartanBitmap=BitmapFactory.decodeStream(input);。。。在webBitmap=BitmapFactory.decodeStream(输入)行之后,感谢您的提示。它没有解决我遇到的问题,但进行检查确实很好。谢谢,通过查看类,我发现它在哪里引发异常。它在哪里引发异常?它在BitmapTextureAtlas.java类中引发。在WriteTextRetoHardware方法中(最终GL10 pGL)。特别是这一行:
if(bitmap==null){//Exception在这里被抛出。}
看起来位图被识别为null。我在我的答案中添加了一个问题的解决方案。只是好奇:那么,你为什么选择这个作为正确的答案呢?