【问题标题】:Is there any android API to get the file type using file format是否有任何 android API 可以使用文件格式获取文件类型
【发布时间】:2015-04-08 06:53:57
【问题描述】:

我正在开发一个 android 应用程序,需要使用文件格式过滤文件。我真正想做的是:

输入:“jpg” => 输出:“图像”

输入:“mp3” => 输出:“音频”

输入:“mov” => 输出:“视频”

是否有任何 android API 可以执行此操作,或者我必须手动将此信息存储在我的应用中。

【问题讨论】:

  • 您可以获得 mime 类型,从中您将能够获得所需的输出。

标签: android


【解决方案1】:

您可以使用MimeTypeMap 类,它会返回类似“text/plain”的内容。 您还可以阅读 mime 类型 here

// the File name or Url
public static String getMimeType(String url)
{
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

【讨论】:

【解决方案2】:

您必须手动执行此操作,如果您需要特定的文件类型,或者如果您想查找文件 mime 类型,您可以使用这样的 mime 库。

public class main {
public static void main(String[] args) {
    String filename = "image.jpg";
    String filenameArray[] = filename.split("\\.");
    String extension = filenameArray[filenameArray.length-1];
    System.out.println(extension);
    if(extension = "jpg"){
        #do something
    }
    }
}

或者

public static String getMimeType(String url){
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多