Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get corresponding file icon in Android?

Tags:

android

Every file type associated with its specific icon.

Now if i am choosing somefilename.xxx then how i could be able to choose the icon associated with *.xxx rather than the default icon for unknown file.

Original Question

How can I get .txt or .doc or .png file icon? For example now I am choosing filename.pdf, how to get filename.pdf icon? For example

File f = new File("filename.pdf");

My File name is filename.pdf. How to get this file icon (Adobereader Icon).

like image 303
Sekar Avatar asked Dec 26 '22 22:12

Sekar


2 Answers

Try Using PackageManager.queryIntentActivities:

final Intent innt = new Intent(Intent.ACTION_VIEW);
innt.setData(fileUri);
innt.setType("application/pdf");

final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(innt, 0);
for (ResolveInfo match : matches) {
    final Drawable icon = match.loadIcon(getPackageManager());
    final CharSequence label = match.loadLabel(getPackageManager());
}
like image 109
ρяσѕρєя K Avatar answered Jan 08 '23 08:01

ρяσѕρєя K


First thing i would like to say any file do not contains it icon with them in general say if you don't have office installed you won't get doc icon or powerpoint icon.

so its system work to associate icon to specific file type so Go this way

To achieve your goal its highly recommended to include all icons that you are expected to use say if You choose some.png check for extention and associate the png.ico to this file icon

doing this way you will have to associate with every type of files.

Edit 1:

you can easily find out the icons on web add to it into your drawable folder's now for every file name extract the extension and set the icon according to the extension this way you can achieve your goal

Edit 2 :

To achieve your goal do the following :

  1. add all icon files to your res\drawable folder for easyness set all icon file name as its extension Say use doc.png for doc extension for easyness

  2. Now map all file extension to your icon file say check for file extension and then do something like following:

String ext = "Something";

switch(ext)    
{    
case "doc":    
    image.setImageResource(R.drawable.doc);    
    break;     
case "exe":    
    image.setImageResource(R.drawable.exe);
    break;
default :
    image.setImageResource(R.drawable.default);    
}
like image 41
3 revs Avatar answered Jan 08 '23 10:01

3 revs