Android: How to check if file is image?
28,587
Solution 1
Try this code.
public class ImageFileFilter implements FileFilter {
private final String[] okFileExtensions = new String[] {
"jpg",
"png",
"gif",
"jpeg"
};
public boolean accept(File file) {
for (String extension: okFileExtensions) {
if (file.getName().toLowerCase().endsWith(extension)) {
return true;
}
}
return false;
}
}
It'll work fine.
Use this like
new ImageFileFilter(pass file name)
;
Solution 2
I think if you want to check whether a file is an image, you need to read it. An image file may not obey the file extension rules. You can try to parse the file by BitmapFactory as following:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
if (options.outWidth != -1 && options.outHeight != -1) {
// This is an image file.
}
else {
// This is not an image file.
}
Related videos on Youtube
Comments
-
Marco Seiz over 3 years
Possible Duplicate:
Know if a file is a image in Java/AndroidHow can I check a file if it is an image? like following:
if(file.isImage)....
If it's not possible with standard libraries, how can I do it with the MagickImage lib?
Thanks in advance!
-
Praful Bhatnagar over 11 yearsmay be you can check with the file extension..
-
My Head Hurts over 11 yearsPossible duplicate stackoverflow.com/q/9244710/681807
-
Marco Seiz over 11 yearsI think it's not the right way to check with file extension. Because I would have to create a function that checks EVERY image file extensions like bmp, jpg, jpeg, png, gif, tif, etc...
-
-
IAmGroot over 11 yearsBut you would need a
try
catch
right?.. -
zsxwing over 11 yearsYou can just check the return value of BitmapFactory.decodeFile(path, options);
-
zsxwing over 11 yearsoptions.inJustDecodeBounds = true decrease the parsing time cost since decodeFile will parse only the width and height of the image.
-
Zala Janaksinh over 11 years@zsxwing if are get system file and witch have no permission to decode then get the null pointer excepetion u can
t read this file and create bitmap.and another thing is when u check the multiple file every time to decode and check it
s increase to generate the outofMemory problem.Just think and then reply. -
Marco Seiz over 11 yearshmm it says false to every image file...
-
Marco Seiz over 11 yearsThat worked... Now I have to search every image extension...
-
Zala Janaksinh over 11 years@MarcoSeiz then flag my comment direct the right way to some one.
-
Baz almost 11 years@zsxwing
decodeFile(String, Options)
will always returnnull
if you setinJustDecodeBounds = true
. Use this check to see if it was decoded successfully:if (options.outWidth != -1 && options.outHeight != -1)
-
Baz almost 11 years@ZalaJanaksinh This would return
true
for any file that has an "image file extension", e.g. if I renametest.mp3
totest.jpg
, your method would returntrue
. -
Allen Vork about 8 yearsit's not the right way. Because not all the path includes the image type is a image
-
Zala Janaksinh about 8 years@AllenVork then suggest what is the right way? && what is the drawback of use of that ?
-
Allen Vork about 8 yearstry the solution below
-
Allen Vork almost 8 yearsThis is a right way to check whether a file is image.But it's a waste of time. I want another way to check a file effectively.
-
zsxwing almost 8 yearsIf you can make sure the file extensions are set correctly, checking the file extension name is the most effective way AFAIK.
-
Reaz Murshed over 7 yearszsxwing's answer works perfectly.
-
Farid over 3 years"
BitmapFactory.decodeFile()
" great way to kill your app if check includes loads of files