How to get corresponding file icon in Android?

后端 未结 3 993
有刺的猬
有刺的猬 2021-02-10 16:57

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 as

相关标签:
3条回答
  • 2021-02-10 17:23

    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());
    }
    
    0 讨论(0)
  • 2021-02-10 17:27

    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);    
    }
    
    0 讨论(0)
  • 2021-02-10 17:45

    You can create a function that takes in a path, and by the file extension in its name, corresponds to the drawable.

    fun getFileTypeIcon(path: String): Int {
        val file = File(path)
        val ext = MimeTypeMap.getFileExtensionFromUrl(file.name)
        Log.i("ImagesExt", "$path ----> $ext")
        return when (".$ext") {
            ".png", ".jpg", ".jpeg", ".gif", ".bmp" -> R.drawable.image
            ".mp3", ".wav", ".ogg", "midi" -> R.drawable.audio
            ".mp4", ".rmvb", ".avi", ".flv", ".3gp" -> R.drawable.video
            ".jsp", ".html", ".htm", ".js", ".php" -> R.drawable.web
            ".txt", ".c", ".cpp", ".xml", ".py", ".json", ".log" -> R.drawable.file
            ".xls", ".xlsx" -> R.drawable.file
            ".doc", ".docx" -> R.drawable.file
            ".ppt", ".pptx" -> R.drawable.file
            ".pdf" -> R.drawable.pdf
            ".jar", ".zip", ".rar", ".gz" -> R.drawable.zip
            ".apk" -> R.drawable.apk
            else -> R.drawable.file
        }
    }
    
    0 讨论(0)
提交回复
热议问题