How to get corresponding file icon in Android?

后端 未结 3 1007
有刺的猬
有刺的猬 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: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
        }
    }
    

提交回复
热议问题