Example usage for android.webkit MimeTypeMap getSingleton

List of usage examples for android.webkit MimeTypeMap getSingleton

Introduction

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

Prototype

public static MimeTypeMap getSingleton() 

Source Link

Document

Get the singleton instance of MimeTypeMap.

Usage

From source file:com.qiscus.sdk.presenter.QiscusChatPresenter.java

public void downloadFile(final QiscusComment qiscusComment) {
    if (qiscusComment.isDownloading()) {
        return;//from w  ww . ja va 2  s .co  m
    }

    File file = Qiscus.getDataStore().getLocalPath(qiscusComment.getId());
    if (file == null) {
        qiscusComment.setDownloading(true);
        QiscusApi.getInstance()
                .downloadFile(qiscusComment.getTopicId(), qiscusComment.getAttachmentUri().toString(),
                        qiscusComment.getAttachmentName(),
                        percentage -> qiscusComment.setProgress((int) percentage))
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle()).doOnNext(file1 -> {
                    if (QiscusImageUtil.isImage(file1)) {
                        QiscusImageUtil.addImageToGallery(file1);
                    }
                    qiscusComment.setDownloading(false);
                    Qiscus.getDataStore().addOrUpdateLocalPath(qiscusComment.getTopicId(),
                            qiscusComment.getId(), file1.getAbsolutePath());
                }).subscribe(file1 -> {
                    view.refreshComment(qiscusComment);
                    if (qiscusComment.getType() == QiscusComment.Type.AUDIO) {
                        qiscusComment.playAudio();
                    } else if (qiscusComment.getType() == QiscusComment.Type.FILE) {
                        view.onFileDownloaded(file1, MimeTypeMap.getSingleton()
                                .getMimeTypeFromExtension(qiscusComment.getExtension()));
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    qiscusComment.setDownloading(false);
                    view.showError("Failed to download file!");
                });
    } else {
        if (qiscusComment.getType() == QiscusComment.Type.AUDIO) {
            qiscusComment.playAudio();
        } else if (qiscusComment.getType() == QiscusComment.Type.IMAGE) {
            view.startPhotoViewer(qiscusComment);
        } else {
            view.onFileDownloaded(file,
                    MimeTypeMap.getSingleton().getMimeTypeFromExtension(qiscusComment.getExtension()));
        }
    }
}

From source file:net.gsantner.opoc.util.ContextUtils.java

/**
 * Detect MimeType of given file/*from w  w w  .j ava 2  s  . c  o m*/
 * 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(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;
}

From source file:com.synox.android.files.services.FileUploader.java

private OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType,
        FileDataStorageManager storageManager) {

    // MIME type/* w  w w.  j a  va  2 s  .  c  o m*/
    if (mimeType == null || mimeType.length() <= 0) {
        try {
            mimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(remotePath.substring(remotePath.lastIndexOf('.') + 1));
        } catch (IndexOutOfBoundsException e) {
            Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + remotePath);
        }
    }
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }

    if (isPdfFileFromContentProviderWithoutExtension(localPath, mimeType)) {
        remotePath += FILE_EXTENSION_PDF;
    }

    OCFile newFile = new OCFile(remotePath);
    newFile.setStoragePath(localPath);
    newFile.setLastSyncDateForProperties(0);
    newFile.setLastSyncDateForData(0);

    // size
    if (localPath != null && localPath.length() > 0) {
        File localFile = new File(localPath);
        newFile.setFileLength(localFile.length());
        newFile.setLastSyncDateForData(localFile.lastModified());
    } // don't worry about not assigning size, the problems with localPath
      // are checked when the UploadFileOperation instance is created

    newFile.setMimetype(mimeType);

    return newFile;
}

From source file:com.cerema.cloud2.files.services.FileUploader.java

private OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType) {

    // MIME type//from w w w. ja  va2s. c  o m
    if (mimeType == null || mimeType.length() <= 0) {
        try {
            mimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(remotePath.substring(remotePath.lastIndexOf('.') + 1));
        } catch (IndexOutOfBoundsException e) {
            Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + remotePath);
        }
    }
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }

    if (isPdfFileFromContentProviderWithoutExtension(localPath, mimeType)) {
        remotePath += FILE_EXTENSION_PDF;
    }

    OCFile newFile = new OCFile(remotePath);
    newFile.setStoragePath(localPath);
    newFile.setLastSyncDateForProperties(0);
    newFile.setLastSyncDateForData(0);

    // size
    if (localPath != null && localPath.length() > 0) {
        File localFile = new File(localPath);
        newFile.setFileLength(localFile.length());
        newFile.setLastSyncDateForData(localFile.lastModified());
    } // don't worry about not assigning size, the problems with localPath
      // are checked when the UploadFileOperation instance is created

    newFile.setMimetype(mimeType);

    return newFile;
}

From source file:org.zywx.wbpalmstar.engine.universalex.EUExWidget.java

public void share(String inShareTitle, String inSubject, String inContent) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt"));
    intent.putExtra(Intent.EXTRA_SUBJECT, inSubject);
    intent.putExtra(Intent.EXTRA_TEXT, inContent);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mContext.startActivity(Intent.createChooser(intent, inShareTitle));
}

From source file:org.mozilla.gecko.GeckoAppShell.java

static String getExtensionFromMimeType(String aMimeType) {
    return MimeTypeMap.getSingleton().getExtensionFromMimeType(aMimeType);
}

From source file:org.mozilla.gecko.GeckoAppShell.java

static String getMimeTypeFromExtensions(String aFileExt) {
    MimeTypeMap mtm = MimeTypeMap.getSingleton();
    StringTokenizer st = new StringTokenizer(aFileExt, "., ");
    String type = null;//from   w w  w.  ja  v a2  s.c o  m
    String subType = null;
    while (st.hasMoreElements()) {
        String ext = st.nextToken();
        String mt = mtm.getMimeTypeFromExtension(ext);
        if (mt == null)
            continue;
        int slash = mt.indexOf('/');
        String tmpType = mt.substring(0, slash);
        if (!tmpType.equalsIgnoreCase(type))
            type = type == null ? tmpType : "*";
        String tmpSubType = mt.substring(slash + 1);
        if (!tmpSubType.equalsIgnoreCase(subType))
            subType = subType == null ? tmpSubType : "*";
    }
    if (type == null)
        type = "*";
    if (subType == null)
        subType = "*";
    return type + "/" + subType;
}

From source file:org.zywx.wbpalmstar.engine.universalex.EUExWidget.java

public void openFile(String path) {
    if (null == path || path.trim().length() == 0) {
        return;/* ww w  . ja  v  a2s.  c  o m*/
    }
    Intent in = new Intent(Intent.ACTION_VIEW);
    MimeTypeMap type = MimeTypeMap.getSingleton();
    String mime = MimeTypeMap.getFileExtensionFromUrl(path);
    mime = type.getMimeTypeFromExtension(mime);
    if (null != mime && mime.length() != 0) {
        File file = new File(path);
        Uri ri = Uri.fromFile(file);
        in.setDataAndType(ri, mime);
    }
    if (appExist(in)) {
        mContext.startActivity(Intent.createChooser(in, "choose one:"));
    } else {
        ;
    }
}

From source file:com.vuze.android.remote.fragment.FilesFragment.java

@SuppressWarnings("unused")
protected boolean reallyStreamFile(Map<?, ?> selectedFile) {
    final String contentURL = getContentURL(selectedFile);
    if (contentURL != null && contentURL.length() > 0) {
        Uri uri = Uri.parse(contentURL);

        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        String name = MapUtils.getMapString(selectedFile, "name", "video");
        intent.putExtra("title", name);

        String extension = MimeTypeMap.getFileExtensionFromUrl(contentURL).toLowerCase(Locale.US);
        String mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mimetype != null && tryLaunchWithMimeFirst) {
            intent.setType(mimetype);//from  w  w  w  .j av  a2 s.  c  om
        }
        Class<?> fallBackIntentClass = VideoViewer.class;
        if (mimetype != null && mimetype.startsWith("image")) {
            fallBackIntentClass = ImageViewer.class;
        }

        final PackageManager packageManager = getActivity().getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (AndroidUtils.DEBUG) {
            Log.d(TAG, "num intents " + list.size());
            for (ResolveInfo info : list) {
                ComponentInfo componentInfo = AndroidUtils.getComponentInfo(info);
                Log.d(TAG, info.toString() + "/"
                        + (componentInfo == null ? "null" : (componentInfo.name + "/" + componentInfo)));
            }
        }
        if (list.size() == 0) {
            // Intent will launch, but show message to the user:
            // "Opening web browser links is not supported"
            intent.setClass(getActivity(), fallBackIntentClass);
        }
        if (list.size() == 1) {
            ResolveInfo info = list.get(0);
            ComponentInfo componentInfo = AndroidUtils.getComponentInfo(info);
            if (componentInfo != null && componentInfo.name != null) {
                if ("com.amazon.unifiedshare.actionchooser.BuellerShareActivity".equals(componentInfo.name)
                        || componentInfo.name.startsWith("com.google.android.tv.frameworkpackagestubs.Stubs")) {
                    intent.setClass(getActivity(), fallBackIntentClass);
                }
            }
        }

        try {
            startActivity(intent);
            if (AndroidUtils.DEBUG) {
                Log.d(TAG, "Started " + uri + " MIME: " + intent.getType());
            }
        } catch (java.lang.SecurityException es) {
            if (AndroidUtils.DEBUG) {
                Log.d(TAG, "ERROR launching. " + es.toString());
            }

            if (mimetype != null) {
                try {
                    Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
                    intent2.putExtra("title", name);
                    if (!tryLaunchWithMimeFirst) {
                        intent2.setType(mimetype);
                    }

                    list = packageManager.queryIntentActivities(intent2, PackageManager.MATCH_DEFAULT_ONLY);
                    if (AndroidUtils.DEBUG) {
                        Log.d(TAG, "num intents " + list.size());
                        for (ResolveInfo info : list) {
                            ComponentInfo componentInfo = AndroidUtils.getComponentInfo(info);
                            Log.d(TAG, info.toString() + "/" + (componentInfo == null ? "null"
                                    : (componentInfo.name + "/" + componentInfo)));
                        }
                    }

                    startActivity(intent2);
                    if (AndroidUtils.DEBUG) {
                        Log.d(TAG,
                                "Started with" + (intent2.getType() == null ? " no" : " ") + " mime: " + uri);
                    }
                    return true;
                } catch (Throwable ex2) {
                    if (AndroidUtils.DEBUG) {
                        Log.d(TAG, "no intent for view. " + ex2.toString());
                    }
                }
            }

            Toast.makeText(getActivity().getApplicationContext(),
                    getActivity().getResources().getString(R.string.intent_security_fail), Toast.LENGTH_LONG)
                    .show();
        } catch (android.content.ActivityNotFoundException ex) {
            if (AndroidUtils.DEBUG) {
                Log.d(TAG, "no intent for view. " + ex.toString());
            }

            if (mimetype != null) {
                try {
                    Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
                    intent2.putExtra("title", name);
                    if (!tryLaunchWithMimeFirst) {
                        intent2.setType(mimetype);
                    }
                    startActivity(intent2);
                    if (AndroidUtils.DEBUG) {
                        Log.d(TAG, "Started (no mime set) " + uri);
                    }
                    return true;
                } catch (android.content.ActivityNotFoundException ex2) {
                    if (AndroidUtils.DEBUG) {
                        Log.d(TAG, "no intent for view. " + ex2.toString());
                    }
                }
            }

            Toast.makeText(getActivity().getApplicationContext(),
                    getActivity().getResources().getString(R.string.no_intent), Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    return true;
}

From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java

/**
 * Looks up the mime type of a given file name.
 *
 * @param filename/*  ww w .ja v a2 s .c  om*/
 * @return a mime type
 */
public static String getMimeType(String filename) {
    MimeTypeMap map = MimeTypeMap.getSingleton();
    return map.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filename));
}