如何使用`URLUtil`获取文件名?

问题描述 投票:1回答:1

下载前如何猜测可能的文件名?我正在使用String nameOfFile = URLUtil.guessFileName(url, contentDisposition, mimetype);,但它仅返回文件名,而不是扩展名。扩展名总是返回“ .bin”。我该如何解决?我在互联网上到处搜索。

java android parsing url
1个回答
0
投票

Note:如果文件没有扩展名,则URLUtil自动将.bin添加到文件中

String nameOfFile = URLUtil.guessFileName(Url, null, null);

    Intent intent = new Intent(Intent.ACTION_VIEW);

    if (guessedFileName.contains(".gif")) {
        // GIF file
        mimeType = "image/gif";
    } else if (guessedFileName.contains(".jpg") || guessedFileName.contains(".jpeg")) {
        // JPG file
        mimeType = "image/jpeg";
    } else if (guessedFileName.contains(".png")) {
        // PNG file
        mimeType = "image/png";
    } else if (guessedFileName.contains(".txt")) {
        // Text file
        mimeType = "text/plain";
    } else if (
        guessedFileName.contains(".mpg") ||
        guessedFileName.contains(".mpeg") ||
        guessedFileName.contains(".mpe") ||
        guessedFileName.contains(".mp4") ||
        guessedFileName.contains(".avi") ||
        guessedFileName.contains(".3gp") ||
        guessedFileName.contains(".3gpp")  ||
        guessedFileName.contains(".3g2")
    ) {
        // Video files
        mimeType = "video/*";
    } else if (guessedFileName.contains(".doc") || guessedFileName.contains(".docx")) {
        // Word document
        mimeType = "application/msword";
    } else if (guessedFileName.contains(".pdf")) {
        // PDF file
        mimeType = "application/pdf";
    } else if (guessedFileName.contains(".ppt") || guessedFileName.contains(".pptx")) {
        // Powerpoint file
        mimeType = "application/vnd.ms-powerpoint";
    } else if (guessedFileName.contains(".xls") || guessedFileName.contains(".xlsx")) {
        // Excel file
        mimeType = "application/vnd.ms-excel";
    } else if (guessedFileName.contains(".rtf")) {
        // RTF file
        mimeType = "application/rtf";
    } else if (guessedFileName.contains(".apk")) {
        mimeType = "application/vnd.android.package-archive";
    } else if (guessedFileName.contains(".eml")) {
        mimeType = "message/rfc822";
    }

提供简要说明:Here

© www.soinside.com 2019 - 2024. All rights reserved.