Java Code Examples for android.webkit.MimeTypeMap#getFileExtensionFromUrl()

The following examples show how to use android.webkit.MimeTypeMap#getFileExtensionFromUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlobModule.java    From react-native-GPay with MIT License 6 votes vote down vote up
private String getMimeTypeFromUri(Uri contentUri) {
  String type = getReactApplicationContext().getContentResolver().getType(contentUri);

  if (type == null) {
    String ext = MimeTypeMap.getFileExtensionFromUrl(contentUri.getPath());
    if (ext != null) {
      type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    }
  }

  if (type == null) {
    type = "";
  }

  return type;
}
 
Example 2
Source File: RNDocViewerModule.java    From react-native-doc-viewer with MIT License 6 votes vote down vote up
/**
 * Returns the MIME Type of the file by looking at file name extension in
 * the URL.
 *
 * @param url
 * @return
 */
private static String getMimeType(String url) {
    String mimeType = null;
    System.out.println("Url: " + url);
    url = url.replaceAll(" ", "");
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }

    if (mimeType == null) {
        mimeType = "application/pdf";
        System.out.println("Mime Type (default): " + mimeType);
    }

    return mimeType;
}
 
Example 3
Source File: MainFragment.java    From traccar-manager-android with Apache License 2.0 6 votes vote down vote up
public String getMimeType(String url) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        switch (extension) {
            case "js":
                return "text/javascript";
            case "woff":
                return "application/font-woff";
            case "woff2":
                return "application/font-woff2";
            case "ttf":
                return "application/x-font-ttf";
            case "eot":
                return "application/vnd.ms-fontobject";
            case "svg":
                return "image/svg+xml";
            default:
                return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
    }
    return null;
}
 
Example 4
Source File: GitHubHelper.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isArchive(@Nullable String name) {
    if (StringUtils.isBlank(name)) return false;
    name = name.toLowerCase();
    for (String value : ARCHIVE_EXTENSIONS) {
        String extension = MimeTypeMap.getFileExtensionFromUrl(name);
        if ((extension != null && value.replace(".", "").equals(extension))
                || name.endsWith(value))
            return true;
    }

    return false;
}
 
Example 5
Source File: BluetoothServer.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
public static String getMimeTypeForFile(String uri) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(uri);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}
 
Example 6
Source File: FileHelper.java    From simple_share with MIT License 5 votes vote down vote up
private String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}
 
Example 7
Source File: Utils.java    From sandriosCamera with MIT License 5 votes vote down vote up
public static String getMimeType(String url) {
    String type = "";
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (!TextUtils.isEmpty(extension)) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    } else {
        String reCheckExtension = MimeTypeMap.getFileExtensionFromUrl(url.replaceAll("\\s+", ""));
        if (!TextUtils.isEmpty(reCheckExtension)) {
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(reCheckExtension);
        }
    }
    return type;
}
 
Example 8
Source File: Helpers.java    From SimplicityBrowser with MIT License 5 votes vote down vote up
public static String getMimeType(String url) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return null;
}
 
Example 9
Source File: ShareFiles.java    From react-native-share with MIT License 5 votes vote down vote up
/**
 * Obtain mime type from URL
 * @param url {@link String}
 * @return {@link String} mime type
 */
private String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}
 
Example 10
Source File: FileUtils.java    From AdBlockedWebView-Android with MIT License 5 votes vote down vote up
public static long downloadFile(Context context, String url, String mimeType) {
    try {
        String[] fnm = url.split("/");
        String fileName = fnm[fnm.length - 1];
        fileName = getFileName(fileName);
        String host = fnm[2];
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if(extension.equals(""))
            extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
        String fileNameWithExtension = fileName + "." + extension;

        DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.setTitle(fileNameWithExtension);
        request.setMimeType(mimeType);
        request.setDescription(host);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileNameWithExtension);
        File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        if (!downloadsDir.exists()) {
            //noinspection ResultOfMethodCallIgnored
            downloadsDir.mkdirs();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        } else {
            //noinspection deprecation
            request.setShowRunningNotification(true);
        }
        request.setVisibleInDownloadsUi(true);
        long downloadId = dm.enqueue(request);

        Toast.makeText(context, context.getString(R.string.message_download_started), Toast.LENGTH_SHORT).show();

        return downloadId;
    } catch (SecurityException e) {
        throw new SecurityException("No permission allowed: android.permission.WRITE_EXTERNAL_STORAGE");
    }
}
 
Example 11
Source File: StreamSource.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public StreamSource(SmbFile file,long l) throws SmbException {

        fp = 0;
        len = l;
        mime = MimeTypeMap.getFileExtensionFromUrl(file.getName());
        name = file.getName();
        this.file = file;
        bufferSize = 1024*60;
    }
 
Example 12
Source File: FileBinary.java    From LiveSourceCode with Apache License 2.0 5 votes vote down vote up
@Override
public String mimeType() {
    String type = "application/octet-stream";
    String name = file.getName();
    // 判断文件是否有扩展名,有则根据扩展名拿到type。
    if (MimeTypeMap.getSingleton().hasExtension(name)) {
        String extension = MimeTypeMap.getFileExtensionFromUrl(name);
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}
 
Example 13
Source File: ImageViewerActivity.java    From android-imageviewer with Apache License 2.0 4 votes vote down vote up
public void initData() {
        imageLoader = ImageLoader.getInstance();
        imageUrls = new ArrayList<String>();

        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                imageUrls = bundle.getStringArrayList(Extra.IMAGES);
                imagePosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
                imageMode = bundle.getInt(Extra.IMAGE_MODE, 0);
                Log.i("The snowdream bundle path of the image is: " + imageUri);
            }

            Uri uri = (Uri) intent.getData();
            if (uri != null) {
                imageUri = uri.getPath();
                fileName = uri.getLastPathSegment();
                getSupportActionBar().setSubtitle(fileName);
                Log.i("The path of the image is: " + imageUri);

                File file = new File(imageUri);

                imageUri = "file://" + imageUri;
                File dir = file.getParentFile();
                if (dir != null) {
                    FileFilter fileFilter = new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            if (f != null) {
                                String extension = MimeTypeMap.getFileExtensionFromUrl(Uri.encode(f.getAbsolutePath()));
                                if (!TextUtils.isEmpty(extension)) {
                                    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                                    if (!TextUtils.isEmpty(mimeType) && mimeType.contains("image")) {
                                        return true;
                                    }
                                }
                            }
                        return false;
                    }
                } ;

                File[] files = dir.listFiles(fileFilter);

                if (files != null && files.length > 0) {
                    int size = files.length;

                    for (int i = 0; i < size; i++) {
                        imageUrls.add("file://" + files[i].getAbsolutePath());
                    }
                    imagePosition = imageUrls.indexOf(imageUri);
                    imageMode = 1;
                    Log.i("Image Position:" + imagePosition);
                }

            } else {
                imageUrls.add("file://" + imageUri);
                imagePosition = 0;
                imageMode = 0;
            }
        }
    }

    else

    {
        Log.w("The intent is null!");
    }

}
 
Example 14
Source File: BaseImageDownloader.java    From WliveTV with Apache License 2.0 4 votes vote down vote up
private boolean isVideoFileUri(String uri) {
	String extension = MimeTypeMap.getFileExtensionFromUrl(uri);
	String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
	return mimeType != null && mimeType.startsWith("video/");
}
 
Example 15
Source File: GitHubHelper.java    From OpenHub with GNU General Public License v3.0 4 votes vote down vote up
public static String getExtension(@Nullable String name) {
    if (StringUtils.isBlank(name)) return null;
    return MimeTypeMap.getFileExtensionFromUrl(name);
}
 
Example 16
Source File: ContextUtils.java    From openlauncher with Apache License 2.0 4 votes vote down vote up
/**
 * Detect MimeType of given file
 * Android/Java's own MimeType map is very very small and detection barely works at all
 * Hence use custom map for some file extensions
 */
public String getMimeType(final Uri uri) {
    String mimeType = null;
    if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        ContentResolver cr = _context.getContentResolver();
        mimeType = cr.getType(uri);
    } else {
        String ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase());

        // Try to guess if the recommended methods fail
        if (TextUtils.isEmpty(mimeType)) {
            switch (ext) {
                case "md":
                case "markdown":
                case "mkd":
                case "mdown":
                case "mkdn":
                case "mdwn":
                case "rmd":
                    mimeType = "text/markdown";
                    break;
                case "yaml":
                case "yml":
                    mimeType = "text/yaml";
                    break;
                case "json":
                    mimeType = "text/json";
                    break;
                case "txt":
                    mimeType = "text/plain";
                    break;
            }
        }
    }

    if (TextUtils.isEmpty(mimeType)) {
        mimeType = "*/*";
    }
    return mimeType;
}
 
Example 17
Source File: FileUtil.java    From pandora with Apache License 2.0 4 votes vote down vote up
public static String getFileType(String filePath) {
    String ext = MimeTypeMap.getFileExtensionFromUrl(filePath);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
}
 
Example 18
Source File: ProxyCacheUtils.java    From AndroidVideoCache with Apache License 2.0 4 votes vote down vote up
static String getSupposablyMime(String url) {
    MimeTypeMap mimes = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    return TextUtils.isEmpty(extension) ? null : mimes.getMimeTypeFromExtension(extension);
}
 
Example 19
Source File: ProxyCacheUtils.java    From GSYVideoPlayer with Apache License 2.0 4 votes vote down vote up
static String getSupposablyMime(String url) {
    MimeTypeMap mimes = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    return TextUtils.isEmpty(extension) ? null : mimes.getMimeTypeFromExtension(extension);
}
 
Example 20
Source File: MediaType.java    From AndServer with Apache License 2.0 2 votes vote down vote up
/**
 * Get he extension by url.
 *
 * @param url url.
 *
 * @return extension name.
 */
public static String getUrlExtension(String url) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    return TextUtils.isEmpty(extension) ? "" : extension;
}