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:com.android.mms.ui.MessageUtils.java

public static String getContentType(Uri uri) {
    String path = uri.getPath();/*from   ww w .  j a v  a  2 s. co  m*/

    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(path).toLowerCase();
    if (TextUtils.isEmpty(extension)) {
        int dotPos = path.lastIndexOf('.');
        if (0 <= dotPos) {
            extension = path.substring(dotPos + 1);
            extension = extension.toLowerCase();
        }
    }

    String type = mimeTypeMap.getMimeTypeFromExtension(extension);
    return type;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * @inheritDoc/*w  w w.  ja  va 2 s  .co m*/
 */
@Override
public Media createMedia(InputStream stream, String mimeType, final Runnable onCompletion) throws IOException {
    if (getActivity() == null) {
        return null;
    }
    /*if(!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")){
    return null;
    }*/
    boolean isVideo = mimeType.contains("video");

    if (!isVideo && stream instanceof FileInputStream) {
        MediaPlayer player = new MediaPlayer();
        player.setDataSource(((FileInputStream) stream).getFD());
        player.prepare();
        return new Audio(getActivity(), player, stream, onCompletion);
    }
    String extension = MimeTypeMap.getFileExtensionFromUrl(mimeType);
    final File temp = File.createTempFile("mtmp", extension == null ? "dat" : extension);
    temp.deleteOnExit();
    OutputStream out = createFileOuputStream(temp);

    byte buf[] = new byte[256];
    int len = 0;
    while ((len = stream.read(buf, 0, buf.length)) > -1) {
        out.write(buf, 0, len);
    }
    out.close();
    stream.close();

    final Runnable finish = new Runnable() {

        @Override
        public void run() {
            if (onCompletion != null) {
                Display.getInstance().callSerially(onCompletion);

                // makes sure the file is only deleted after the onCompletion was invoked
                Display.getInstance().callSerially(new Runnable() {
                    @Override
                    public void run() {
                        temp.delete();
                    }
                });
                return;
            }
            temp.delete();
        }
    };

    if (isVideo) {
        final AndroidImplementation.Video[] retVal = new AndroidImplementation.Video[1];
        final boolean[] flag = new boolean[1];

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                VideoView v = new VideoView(getActivity());
                v.setZOrderMediaOverlay(true);
                v.setVideoURI(Uri.fromFile(temp));
                retVal[0] = new AndroidImplementation.Video(v, getActivity(), finish);
                flag[0] = true;
                synchronized (flag) {
                    flag.notify();
                }
            }
        });
        while (!flag[0]) {
            synchronized (flag) {
                try {
                    flag.wait(100);
                } catch (InterruptedException ex) {
                }
            }
        }

        return retVal[0];
    } else {
        return createMedia(createFileInputStream(temp), mimeType, finish);
    }

}