Check if file is valid image on Android

Check if file is valid image on Android
0.0 0


Hi all!

I’m downloading image file from the internet and using this file to create new texture with addImage function.
CCTexture2D *newTexture = CCTextureCache::sharedTextureCache()->addImage(sFileName.c_str());
But sometimes file is not valid image (e.g. text or html). In that case addImage causes crash. Try-catch block does not help. So, my question is:

  • Is there any way to check if file is valid image?

Thanks in advance!


Hi Alexander,
Working with CCSprite there are several init funtions that returns a bool, that allows you to check if initialization is right before working with it.
I’ve seen that CCTexture2D also has init functions, though CCTextureCache doesn’t.

Hope you find usefull one ot then,
Jordi :slight_smile:


Thank you for reply, Jordi! But CCTexture2D init also crashes for me. I believe CCSprite init will call CCTexture2D’s init.


Well… one thing you can do is check the image initialization throught CCSprite, and if it’s valid load it into CCTextureCache.


I try with CCImage’s initWithFile and it works. When I try to turn test.txt into a CCImage, it return false without crash.:slight_smile:


Good! Try checking “text.jpg” without changing content of the file. Most likely it will crash. But thank you for info! I will double check my sources and will try initWithFile, it’s possible that I have missed something.