且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中获取相应的文件图标?

更新时间:2023-09-29 22:50:04

第一件事,我想说任何文件通常都不包含图标,说如果您不要没有办公室已安装您将不会获得文档图标或PowerPoint图标.

因此它的系统可以将图标与特定文件类型相关联,因此请按这种方式

为实现您的目标,强烈建议您包含预期使用的所有图标说出如果您选择 some.png 进行扩展检查并将 png.ico 与此文件图标相关联

这样做,您将不得不与每种类型的文件相关联.

您可以轻松地在网络上找到图标,然后将其添加到可绘制文件夹的每个文件名中,提取扩展名并根据扩展名设置图标这样,您就可以实现自己的目标

要实现您的目标,请执行以下操作:

  1. 将所有图标文件添加到您的res \ drawable文件夹中,以方便设置所有图标文件名称作为其扩展名说 doc.png 用于 doc扩展 为方便起见

  2. 现在将所有文件扩展名映射到您的图标文件,说检查文件扩展,然后执行以下操作:

String ext =东西";

 开关(ext){案例"doc":image.setImageResource(R.drawable.doc);休息;案例"exe":image.setImageResource(R.drawable.exe);休息;默认 :image.setImageResource(R.drawable.default);} 

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).

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);    
}