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:org.matrix.androidsdk.db.MXMediaDownloadWorkerTask.java

/**
 * Build a filename from an url/*from   w w w.j a  v a2s . c o m*/
 * @param Url the media url
 * @param mimeType the mime type;
 * @return the cache filename
 */
public static String buildFileName(String Url, String mimeType) {
    String name = "file_" + MXMediaDownloadWorkerTask.uniqueId(Url);

    if (!TextUtils.isEmpty(mimeType)) {
        String fileExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);

        // some devices don't support .jpeg files
        if ("jpeg".equals(fileExtension)) {
            fileExtension = "jpg";
        }

        if (null != fileExtension) {
            name += "." + fileExtension;
        }
    }

    return name;
}

From source file:com.mifos.mifosxdroid.dialogfragments.DocumentDialogFragment.java

public void uploadFile() {

    String[] parts = fileChoosen.getName().split("\\.");
    Log.d(LOG_TAG, "Extension :" + parts[1]);
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(parts[1]);
    Log.d(LOG_TAG, "Mime Type = " + mimeType);

    TypedFile typedFile = new TypedFile(mimeType, fileChoosen);

    safeUIBlockingUtility.safelyBlockUI();
    App.apiManager.createDocument(entityType, entityId, documentName, documentDescription, typedFile,
            new Callback<GenericResponse>() {
                @Override//w  ww. java2 s. c o  m
                public void success(GenericResponse genericResponse, Response response) {

                    if (genericResponse != null) {

                        Toast.makeText(getActivity(),
                                String.format(getString(R.string.uploaded_successfully), fileChoosen.getName()),
                                Toast.LENGTH_SHORT).show();

                        Log.d(LOG_TAG, genericResponse.toString());
                    }
                    safeUIBlockingUtility.safelyUnBlockUI();
                    getDialog().dismiss();

                }

                @Override
                public void failure(RetrofitError retrofitError) {

                    Toast.makeText(getActivity(), getString(R.string.upload_failed), Toast.LENGTH_SHORT).show();
                    getDialog().dismiss();
                    safeUIBlockingUtility.safelyUnBlockUI();
                    getDialog().dismiss();

                }
            });

}

From source file:org.odk.collect.android.widgets.ArbitraryFileWidget.java

public String getMimeType(String url) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    return extension != null ? MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) : null;
}

From source file:net.sf.xfd.provider.ProviderBase.java

@NonNull
public String getTypeFastest(int dirFd, CharSequence name, Stat stat) {
    if (stat.type != null) {
        switch (stat.type) {
        case DIRECTORY:
            return MIME_TYPE_DIR;
        case CHAR_DEV:
            return CHAR_DEV_MIME;
        }/*from w  ww .j av  a  2s  . c o m*/
    }

    final OS os = getOS();
    if (os == null) {
        return DEFAULT_MIME;
    }

    // Do we have an option to open the file? Does it even make sense?
    // It is a bad idea to open sockets, special files and, especially, pipes
    // (this can cause block, take some time or have other unwelcome side-effects).
    // Trying to content-sniff ordinary zero-sized files also does not make sense
    // Unless they are on some special filesystem (such as procfs), that incorrectly
    // reports zero sizes to stat().

    boolean canSniffContent = stat.type == FsType.FILE;

    int fd = Fd.NIL;

    try {
        final boolean isLink = stat.type == FsType.LINK;

        if (isLink) {
            // we must exclude the possibility that this is a symlink to directory

            if (!os.faccessat(dirFd, name, OS.F_OK)) {
                // the link is broken or target is inaccessible, bail
                return LINK_MIME;
            }

            try {
                os.fstatat(dirFd, name, stat, 0);

                switch (stat.type) {
                case DIRECTORY:
                    return MIME_TYPE_DIR;
                case CHAR_DEV:
                    return CHAR_DEV_MIME;
                }
            } catch (IOException ioe) {
                LogUtil.logCautiously("Unable to stat target of " + name, ioe);
            }
        } else {
            try {
                if (canSniffContent) {
                    fd = os.openat(dirFd, name, OS.O_RDONLY, 0);

                    os.fstat(fd, stat);
                } else {
                    os.fstatat(dirFd, name, stat, 0);
                }
            } catch (IOException ioe) {
                LogUtil.logCautiously("Unable to directly stat " + name, ioe);
            }
        }

        final String extension = getExtensionFast(name);

        final MimeTypeMap mimeMap = MimeTypeMap.getSingleton();

        final String foundMime = mimeMap.getMimeTypeFromExtension(extension);
        if (foundMime != null) {
            return foundMime;
        }

        canSniffContent = canSniffContent && (stat.st_size != 0 || isPossiblySpecial(stat));

        if (isLink) {
            // check if link target has a usable extension
            CharSequence resolved = null;

            // Some filesystem (procfs, you!!) do export files as symlinks, but don't allow them
            // to be open via these symlinks. Gotta be careful here.
            if (canSniffContent) {
                try {
                    fd = os.openat(dirFd, name, OS.O_RDONLY, 0);

                    resolved = os.readlinkat(DirFd.NIL, fdPath(fd));
                } catch (IOException ioe) {
                    LogUtil.logCautiously("Unable to open target of " + name, ioe);
                }
            }

            if (resolved == null) {
                try {
                    resolved = os.readlinkat(dirFd, name);

                    if (resolved.charAt(0) == '/') {
                        resolved = canonString(resolved);
                    }
                } catch (IOException linkErr) {
                    return LINK_MIME;
                }
            }

            final String linkTargetExtension = getExtensionFromPath(resolved);

            if (linkTargetExtension != null && !linkTargetExtension.equals(extension)) {
                final String sortaFastMime = mimeMap.getMimeTypeFromExtension(linkTargetExtension);
                if (sortaFastMime != null) {
                    return sortaFastMime;
                }
            }

            // let's try to open by resolved name too, see above
            name = resolved;
        }

        if (canSniffContent) {
            if (fd < 0) {
                fd = os.openat(dirFd, name, OS.O_RDONLY, 0);
            }

            final String contentInfo = magic.guessMime(fd);

            if (contentInfo != null) {
                return contentInfo;
            }
        }
    } catch (IOException ioe) {
        LogUtil.logCautiously("Failed to guess type of " + name, ioe);
    } finally {
        if (fd > 0) {
            os.dispose(fd);
        }
    }

    if (stat.type == null) {
        return DEFAULT_MIME;
    }

    switch (stat.type) {
    case LINK:
        return LINK_MIME;
    case DOMAIN_SOCKET:
        return SOCK_MIME;
    case NAMED_PIPE:
        return FIFO_MIME;
    case FILE:
        if (stat.st_size == 0) {
            return EMPTY_MIME;
        }
    default:
        return DEFAULT_MIME;
    }
}

From source file:com.hippo.nimingban.ui.GalleryActivity2.java

/**
 * @param uri the save image file url, null for fail
 * @param share true for share/*w w w  . ja  v a  2  s.  com*/
 */
public void onSaveTaskOver(Uri uri, boolean share) {
    if (mSaveTask != null) {
        mSaveTask = null;
    }

    if (share) {
        if (uri == null) {
            Toast.makeText(this, R.string.cant_save_image, Toast.LENGTH_SHORT).show();
        } else {
            String mimeType = getContentResolver().getType(uri);
            if (TextUtils.isEmpty(mimeType)) {
                mimeType = MimeTypeMap.getSingleton()
                        .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
                if (TextUtils.isEmpty(mimeType)) {
                    mimeType = "image/*";
                }
            }
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.setType(mimeType);
            startActivity(Intent.createChooser(intent, getString(R.string.share_image)));
        }
    } else {
        Toast.makeText(this, uri != null ? R.string.save_successfully : R.string.save_failed,
                Toast.LENGTH_SHORT).show();
    }
}

From source file:com.jaspersoft.android.jaspermobile.activities.storage.fragment.SavedItemsFragment.java

private void openReportFile(File reportOutputFile, String recordUri) {
    Locale current = getResources().getConfiguration().locale;
    String fileName = reportOutputFile.getName();
    String baseName = FileUtils.getBaseName(fileName);
    String extension = FileUtils.getExtension(fileName).toLowerCase(current);

    if ("HTML".equalsIgnoreCase(extension)) {
        // run the html report viewer
        SavedReportHtmlViewerActivity_.intent(this).reportFile(reportOutputFile).resourceLabel(baseName)
                .recordUri(recordUri).start();
    } else {//from  w w  w . j a  va  2  s  . c  o m
        // run external viewer according to the file format
        String contentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        Uri reportOutputPath = Uri.fromFile(reportOutputFile);
        Intent externalViewer = new Intent(Intent.ACTION_VIEW);
        externalViewer.setDataAndType(reportOutputPath, contentType);
        externalViewer.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            startActivity(externalViewer);
        } catch (ActivityNotFoundException e) {
            // show notification if no app available to open selected format
            Toast.makeText(getActivity(), getString(R.string.sdr_t_no_app_available, extension),
                    Toast.LENGTH_SHORT).show();
        }
    }
}

From source file:fileops.FileChooser.java

private void openFile(String file) {
    // TODO Auto-generated method stub
    File mfile = new File(file);
    MimeTypeMap myMime = MimeTypeMap.getSingleton();

    Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);

    // Intent newIntent = new Intent(Intent.ACTION_VIEW);
    String mimeType = myMime.getMimeTypeFromExtension(fileExt(mfile.toString()).substring(1));
    newIntent.setDataAndType(Uri.fromFile(mfile), mimeType);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {// w w  w  . j  a v a  2s .  c o  m
        getActivity().startActivity(newIntent);
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "No handler for this type of file.", 1000).show();
    }
}

From source file:in.neoandroid.neoupdate.neoUpdate.java

private void startUpdateApk(Uri installerUri) {
    MimeTypeMap myMime = MimeTypeMap.getSingleton();
    String mimeType = myMime.getMimeTypeFromExtension("apk");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(installerUri, mimeType);//"application/vnd.android.package-archive");
    context.startActivity(intent);// www .  j  a  va2  s .c  om
}

From source file:activity.DetailsActivity.java

/**
 * Opens the document if it is on storage, else downloads it before open it.
 * /*from ww  w  . j  a v  a2 s .  c om*/
 * @param item
 *            the document to open
 */
public void openFileInMemory(final Document item) {
    mItem = item;
    MimeTypeMap map = MimeTypeMap.getSingleton();
    final String mime = map.getMimeTypeFromExtension(item.getExtension());
    mSubPath = item.getTitle() + "." + item.getExtension();

    if (mime != null) {
        if (item.isOnMemory() && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.fromFile(
                    new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                            .getAbsolutePath() + "/" + mSubPath)),
                    mime.toLowerCase(Locale.US));

            startActivity(Intent.createChooser(i, getString(R.string.dialog_choose_app)));

        } else {
            getService().getDownloadTokenizedUrl(item.getList().getCours().getSysCode(),
                    item.getResourceString(), mDwlManagerHandler);
        }

    } else {
        DetailsActivity.this.getService().getDownloadTokenizedUrl(item.getList().getCours().getSysCode(),
                item.getResourceString(), mTokenizedURLHandler);
    }
}

From source file:com.todoroo.astrid.files.FilesControlSet.java

private void showFile(final TaskAttachment m) {
    final String fileType = m.containsNonNullValue(TaskAttachment.CONTENT_TYPE) ? m.getContentType()
            : TaskAttachment.FILE_TYPE_OTHER;
    final String filePath = m.getFilePath();

    if (fileType.startsWith(TaskAttachment.FILE_TYPE_AUDIO)) {
        play(m.getFilePath(), new PlaybackExceptionHandler() {
            @Override/*  w w w.jav  a2s .c o m*/
            public void playbackFailed() {
                showFromIntent(filePath, fileType);
            }
        });
    } else if (fileType.startsWith(TaskAttachment.FILE_TYPE_IMAGE)) {
        activity.startActivity(new Intent(Intent.ACTION_VIEW) {
            {
                setDataAndType(Uri.fromFile(new File(filePath)), fileType);
            }
        });
    } else {
        String useType = fileType;
        if (fileType.equals(TaskAttachment.FILE_TYPE_OTHER)) {
            String extension = AndroidUtilities.getFileExtension(filePath);

            MimeTypeMap map = MimeTypeMap.getSingleton();
            String guessedType = map.getMimeTypeFromExtension(extension);
            if (!TextUtils.isEmpty(guessedType)) {
                useType = guessedType;
            }
            if (!useType.equals(guessedType)) {
                m.setContentType(useType);
                m.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
                taskAttachmentDao.saveExisting(m);
            }
        }
        showFromIntent(filePath, useType);
    }
}