Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:02:04+00:00 2026-06-17T09:02:04+00:00

For example, I wanted to get the file Extension from the file URL using

  • 0

For example, I wanted to get the file Extension from the file URL using the function below:

File name:

Greatest Hits - Lenny Kravitz (Booklet 01) [2000].jpg

Url of the file:

String url = "/mnt/sdcard/mydev/Greatest Hits - Lenny Kravitz (Booklet 01) [2000].jpg";

Function call:

String extension = MimeTypeMap.getFileExtensionFromUrl(url);

But I’m getting an exception on the function call. Is this a bug or a feature?

It works fine for file names that don’t contain that many foreign characters (such as paranthesis).

Is the function buggy? Am I missing something? How am I supposed to differentiate a bug from a feature? I’ve read the function description and it should work properly.

Do you personally use it in your projects? It doesn’t seem reliable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:02:05+00:00Added an answer on June 17, 2026 at 9:02 am

    When I test your code, no exception is thrown for me. Though the proper file extension “jpg” is not returned. I would not advise using MimeTypeMap. An easy way to obtain the file extension instead is as follows:

    String file = "/mnt/sdcard/mydev/Greatest Hits - Lenny Kravitz (Booklet 01) [2000].jpg";
    String exten = "";
    
    int i = file.lastIndexOf('.');
    if (i > 0) {
        exten = file.substring(i+1);
    }
    

    As to why MimeTypeMap.getFileExtensionFromUrl(url) fails? It’s expecting a properly formated URL String, which yours is not. You should first encode it using URLEncoder. For example:

    String url = "/mnt/sdcard/mydev/Greatest Hits - Lenny Kravitz (Booklet 01) [2000].jpg";
    url = URLEncoder.encode(url, "UTF-8");
    

    This should allow MimeTypeMap.getFileExtensionFromUrl(url) to work properly but unfortunately it still doesn’t. Why? URLEncoder will change all spaces to a ‘+’ sign and getFileExtensionFromUrl considers that an invalid character. This part, IMHO, is a bug.

    From my experience, most people don’t use this method. In fact, I never heard of it until you posted this question. Probably because finding a file extension is fairly trivial and most people write code similar to what I posted above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the example below, if client code using GetPeople wanted to print the name
I wanted to get the words from a cell. For example, cell A2 has
I wanted to get the file extension of online file but problem is that
I wanted to read the content of a file using the read() function. I
Let's say for example I wanted to echo You are using Windows! or You
Is it possible to use perl's move function from File::Copy module to use a
I'm trying to import data from a csv file following the example given by
i have a function that read config file and i wanted to reload this
I have url like this which is a js file example like this link
I'm working on a Chrome extension and wanted to load scripts from localhost for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.