How to get corresponding file icon in Android?

后端 未结 3 772
说谎
说谎 2021-02-10 17:21

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:49

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

提交回复
热议问题