samus
samus

Reputation: 6202

Default System Icons for File Types (Android)

Is there an API that will return an icon for a given file extension?

I need to display a file listing within a sub-view. (These are files "attached" to a record, so simply embedding a file explorer and dumping a directory will not suffice).

I've noticed that the built-in File Manager (Asus) and 3rd party Root Explorer both appear to be displaying the same icon's for types based on their "grouping" (text, images, ...).

For example, all text-files (.txt, .doc) have the same icon, and all image-files (.jpg, .png, ...) have the same icon (the exception to this is ES File Explorer, which has a specific icon for .txt and .doc files).

I've also observed that they each have their own set of icons (the text and image icon's in File Explorer are different that Root Explorer's).

So it appears that the common/best practice in Android is to display files by their content, not the the associated program that opens it (even if I specify a default app to open a certain file type, the icon doesn't appear to change).

To avoid wheel re-invention, I would like to simply leverage an OS provided API that would return an icon when provided a file extension?

Such an API method would provide consistency for any other app that needs to do this (by having uniform icons), and also result in future proofed code, as in the case of the "density profiles" being abandoned at some point (med, lg, xl, xxl, xxxl, ...). I suppose it would need to return an icon formatted for the current screen density as well.

NOTE: I'd rather not refer to icons directly (ie, android:src="@android:drawable/ic_menu_camera") in case the paths to them ever break, but if that is the only way to achieve this then so be it (maybe I'll have to write a wrapper to abstract away).

Upvotes: 0

Views: 2255

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

Is there an API that will return an icon for a given file extension?

Not directly, as Android does not deal with file extensions.

You are welcome to:

  • Use MimeTypeMap to try to derive a MIME type for a file extension
  • Craft an Intent that you would use for content with that MIME type
  • Use PackageManager and resolveActivity() or queryIntentActivities() to find out what activity (or activities) would handle this Intent
  • Use the resulting information, along with PackageManager to get the icon associated with the activity

IOW, you do what home screen launchers do, except that you would not be using a LAUNCHER Intent.

Upvotes: 2

Related Questions