Vous êtes sur la page 1sur 1

Search… Log In Sign Up

Home Android: How to check if file is image? [duplicate] Ask Question

PUBLIC
asked 5 years, 8 months ago
Stack Overflow Possible Duplicate:
Know if a file is a image in Java/Android viewed 18,102 times
Tags 15
active 4 years, 2 months ago
Users How can I check a file if it is an image? like following:

Jobs if(file.isImage)....
Linked
3 If it's not possible with standard libraries, how can I do it with the MagickImage lib?
3 Know if a file is a image in Java/Android
Teams
Q&A for work Thanks in advance!
3 MIME type images/* is showing images and
android image listview imagemagick videos both
Learn More
0 How to convert a file object into a drawable
share improve this question edited May 23 '17 at 11:47 asked Dec 7 '12 at 9:29
Community ♦ Marco Seiz 1 How to check android drawable type?
1 1 439 4 16 38
0 java.file.io is not getting resolved out

marked as duplicate by My Head Hurts, Graham Smith, Don Roby, animuson ♦, Linger Dec 8 '12 Related
at 19:31
This question has been asked before and already has an answer. If those answers do not fully address your 1740 Lazy load of images in ListView
question, please ask a new question.
3134 Close/hide the Android Soft Keyboard

489 Android: combining text & image on a


2 may be you can check with the file extension.. – Praful Bhatnagar Dec 7 '12 at 9:32 Button or ImageButton

1 Possible duplicate stackoverflow.com/q/9244710/681807 – My Head Hurts Dec 7 '12 at 9:33 3108 Why is the Android emulator so slow? How
can we speed up the Android emulator?
I 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... – Marco Seiz Dec 7 '12 at 9:36 2333 Is there a unique Android device ID?

add a comment 582 Detect whether there is an Internet


connection available on Android
2 Answers active oldest votes 3167 Proper use cases for Android
UserManager.isUserAGoat()?

652 How to avoid reverse engineering of an


Try This Code Dear. APK file?

539 Changing image size in Markdown


17 ublic class ImageFileFilter implements FileFilter
{
File file; 922 Android Studio: Add jar as library?
private final String[] okFileExtensions = new String[] {"jpg", "png", "gif","jpeg"};

/** Hot Network Questions


*
*/ How to handle people blocking your view and
public ImageFileFilter(File newfile) breaking the rules at the theater by not sitting in
{ their assigned seats?
this.file=newfile; Random variable defined as A with 50% chance
} and B with 50% chance

public boolean accept(File file) Could a submarine built during the cold war
{ maintain pressure underwater for 50 years?
for (String extension : okFileExtensions) During the Great Depression, did the US Army do
{ a study involving paying people to dig a hole and
if (file.getName().toLowerCase().endsWith(extension)) fill it back up?
{
return true; Why do people not notice our enormous,
} prominent, clear and contrasting purple banner?
} Will mathematics journals accept a non-novel
return false; theorem, but with different approach
}
Why do these chords from the plastic bag scene in
} American Beauty work?

Pages that Magento 2 frontend comprises of

It`s Work fine. Does removing a GUI from a server make it less
vulnerable?
and Use This like (new ImageFileFilter(pass file name)); If everyone in the world disappeared except 35
random people, how long would it take for one of
share improve this answer answered Dec 7 '12 at 9:38 them to realize they're not alone?

Zala Janaksinh Application form asking for number of sick days


1,932 5 24 55
Do wrong beliefs threaten democracy?

How can I legally reduce religion in my world?


4 That worked... Now I have to search every image extension... – Marco Seiz Dec 7 '12 at 10:30
When did SHAEF / Eisenhower move HQ from
Most welCome Dear. – Zala Janaksinh Dec 7 '12 at 10:31
London to France during WWII?
11 @ZalaJanaksinh This would return true for any file that has an "image file extension", e.g. if I rename Making a list of patterns of arbitrary length
test.mp3 to test.jpg , your method would return true . – Baz May 10 '13 at 8:39
What is the difference between tr and tr -s?
it's not the right way. Because not all the path includes the image type is a image – Allen Vork Mar 31 '16 at
6:39 How many cubes can be built

@AllenVork then suggest what is the right way? && what is the drawback of use of that ? – Zala Janaksinh Why these Surahs are used to perform Ruqyah -
Mar 31 '16 at 6:42 Surat al-Kaafirun, Surat al-Ikhlas, Surat al-A’araf,
Surat Yoonus, Surat Ta-Ha
show 2 more comments Immigration line selection at port of entry in the
USA

How can I ask my girlfriend to be my gym buddy


without hurting her feelings?

What does the idiom “funny as hell” mean?

Is it appropriate for a PI to appoint someone who


is a family member?

Is it rude to send a self-correction followup email


I think if you want to check whether a file is an image, you need to read it. An image file may not to professor?
obey the file extension rules. You can try to parse the file by BitmapFactory as following: 1 = 2? A Non-Math Riddle
43
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.
}

share improve this answer edited Sep 5 '13 at 1:43 answered Dec 7 '12 at 9:41
zsxwing
13.9k 4 28 51

1 But you would need a try catch right?.. – Doomsknight Dec 7 '12 at 9:45

You can just check the return value of BitmapFactory.decodeFile(path, options); – zsxwing Dec 7 '12 at 9:48

3 options.inJustDecodeBounds = true decrease the parsing time cost since decodeFile will parse only the width
and height of the image. – zsxwing Dec 7 '12 at 9:57

@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. – Zala Janaksinh Dec 7 '12 at 10:29

3 @zsxwing decodeFile(String, Options) will always return null if you set inJustDecodeBounds =
true . Use this check to see if it was decoded successfully: if (options.outWidth != -1 &&
options.outHeight != -1) – Baz May 10 '13 at 8:37

show 4 more comments

Not the answer you're looking for? Browse other questions tagged android image listview

imagemagick or ask your own question.

STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Engagement Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Contact Us site design / logo © 2018 Stack Exchange Inc; user contributions
licensed under cc by-sa 3.0 with attribution required.
Disable Responsiveness rev 2018.8.27.31416

Vous aimerez peut-être aussi