0

I have the name of a file img2455, but i dont have the extension. It is possible to check the extension of that file?

It can be stored on assets folder as .jpg, .png or .bmp, but i dont know, i must check it. ¿How can i do it?

Now i am accessing the file with a content provider forzing the extension as .jpg, and it works when it is jpg, but it fails when the file haves bmp or png extension. This is my code:

theUri = Uri.parse("content://"+getPackageName()+"/"+file.getName()+".jpg");

thanks

NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

0

Get the file's path or name in a String. Check if it endsWith() ".jpg" or ".png" or ".bmp" and then take appropriate action.

String filePath = "/assets/someFile.jpg";

if (filePath.endsWith(".jpg"))
  Log.d("JPG", "Got a JPG");
else if(filePath.endsWith(".png"))
  Log.d("PNG", "Got a PNG");
else if(filePath.endsWith(".bmp"))
  Log.d("BMP", "Got a BMP");
else Log.d("?", "Got some BS");
Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22