Example usage for android.webkit MimeTypeMap getFileExtensionFromUrl

List of usage examples for android.webkit MimeTypeMap getFileExtensionFromUrl

Introduction

In this page you can find the example usage for android.webkit MimeTypeMap getFileExtensionFromUrl.

Prototype

public static String getFileExtensionFromUrl(String url) 

Source Link

Document

Returns the file extension or an empty string if there is no extension.

Usage

From source file:Main.java

/**
 * To add hack to .3gp file//  ww w . ja va 2s  .  c o  m
 * @param url
 * @return
 */
public static String getAudioMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    Log.w("KKIM", " Sending File Extension = " + extension);
    if (extension != null) {
        if (extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("3gpp")) {
            if (is3gpFileAudio(url)) {
                type = "audio/3gpp";
            } else {
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                type = mime.getMimeTypeFromExtension(extension);
            }
        } else {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension);
        }

        if (type.equalsIgnoreCase("application/ogg")) {
            type = "audio/ogg";
            Log.d("KKIM", "Formatting Audio File Type from application/ogg to audio/ogg");
        }
    }
    return type;
}

From source file:Main.java

public static String getMimeType(String url, String defaultType) {
    MimeTypeMap mtm = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    String mimetype = defaultType;

    if (extension != null) {
        String type = mtm.getMimeTypeFromExtension(extension);
        if (type != null) {
            mimetype = type;/*  www .ja va 2s .  c  om*/
        }
    }

    return mimetype;
}

From source file:Main.java

/**
 * Return the MIME type from the URI/*from   w  w  w .  j  ava2 s.  c  om*/
 * @param context Context
 * @param uri URI
 * @return Return the MIME type
 */
public static String getMimetypeFromUri(Context context, Uri uri) {
    String contentType = context.getContentResolver().getType(uri);
    if (TextUtils.isEmpty(contentType)) {
        final MimeTypeMap type_map = MimeTypeMap.getSingleton();
        // Get the extension from the path
        String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
        extension = extension.toLowerCase();
        if (extension.contains(".")) {
            extension = extension.substring(extension.lastIndexOf("."));
        }
        contentType = type_map.getMimeTypeFromExtension(extension);
    }
    return contentType;
}

From source file:Main.java

public static String getMimeType(String filePath) {
    String type = null;/*from   w ww  .j  a v a  2  s .  co  m*/
    String extension = MimeTypeMap.getFileExtensionFromUrl(filePath);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

From source file:Main.java

public static String getExtensionFromFileName(String fileName) {
    // Figure out the MIME type
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileName);
    if (TextUtils.isEmpty(extension)) {
        /*//from   w  w w.  ja  v a2  s.co  m
         * getFileExtensionFromUrl doesn't work for files with spaces
         */
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex >= 0) {
            extension = fileName.substring(dotIndex + 1);
        }
    }

    return extension;
}

From source file:com.pheromone.plugins.FileUtils.java

/**
 * Looks up the mime type of a given file name.
 *
 * @param filename/*from  w ww  . j  ava2s .c o m*/
 * @return a mime type
 */
public static String getMimeType(String filename) {
    MimeTypeMap map = MimeTypeMap.getSingleton();
    return map.getMimeTypeFromExtension(map.getFileExtensionFromUrl(filename));
}

From source file:Main.java

/**
 * Checks if file passed is an image//w w w .  j av a  2  s.c o  m
 * @param file
 * @return true/false
 */
public static boolean isImage(File file) {
    Uri selectedUri = Uri.fromFile(file);
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString().toLowerCase());
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

    return (mimeType != null && mimeType.startsWith("image/"));
}

From source file:com.phonegap.FileUtils.java

/**
 * Read content of text file and return as base64 encoded data url.
 * //from  w  w  w .java2s  .c  o m
 * @param filename         The name of the file.
 * @return               Contents of file = data:<media type>;base64,<data>
 * @throws FileNotFoundException, IOException
 */
public String readAsDataURL(String filename) throws FileNotFoundException, IOException {
    byte[] bytes = new byte[1000];
    BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int numRead = 0;
    while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
        bos.write(bytes, 0, numRead);
    }

    // Determine content type from file name
    String contentType = null;
    if (filename.startsWith("content:")) {
        Uri fileUri = Uri.parse(filename);
        contentType = this.ctx.getContentResolver().getType(fileUri);
    } else {
        MimeTypeMap map = MimeTypeMap.getSingleton();
        contentType = map.getMimeTypeFromExtension(map.getFileExtensionFromUrl(filename));
    }

    byte[] base64 = Base64.encodeBase64(bos.toByteArray());
    String data = "data:" + contentType + ";base64," + new String(base64);
    return data;
}

From source file:com.proofhq.Open.java

/**
 * Returns the MIME type of the file./*from w  w w . j  a  v  a 2s. c o  m*/
 *
 * @param path
 * @return
 */
private static String getMimeType(String path) {
    String mimeType = null;

    String extension = MimeTypeMap.getFileExtensionFromUrl(path);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        mimeType = mime.getMimeTypeFromExtension(extension);
    }

    System.out.println("Mime type: " + mimeType);

    return mimeType;
}

From source file:Main.java

/**
 * Returns the MIME type of a document, from its URL.
 *
 * @param url the document url/* ww  w  .j a  va 2 s .  co m*/
 * @return the document MIME type
 */
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);
    }
    if (null == type) {
        type = "application/octet-stream";
    }
    return type;
}