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.nbplus.hybrid.BasicWebViewClient.java

public String getMimeType(String url) {
    String type = null;// w w  w .  j a  va2 s. co m
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

From source file:org.sufficientlysecure.keychain.ui.DecryptListFragment.java

@TargetApi(VERSION_CODES.KITKAT)
private void saveFileDialog(InputDataResult result, int index) {

    Activity activity = getActivity();//ww w.j ava2s . c  o m
    if (activity == null) {
        return;
    }

    OpenPgpMetadata metadata = result.mMetadata.get(index);
    mCurrentSaveFileUri = result.getOutputUris().get(index);

    String filename = metadata.getFilename();
    if (TextUtils.isEmpty(filename)) {
        String ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(metadata.getMimeType());
        filename = "decrypted" + (ext != null ? "." + ext : "");
    }

    // requires >=kitkat
    FileHelper.saveDocument(this, filename, metadata.getMimeType(), REQUEST_CODE_OUTPUT);
}

From source file:com.owncloud.android.utils.MimeTypeUtil.java

/**
 * determines the list of possible mime types for the given file, based on its extension.
 *
 * @param filename the file name/*  w  w w  . j  av a2s. co m*/
 * @return list of possible mime types (ordered), empty list in case no mime types found
 */
private static List<String> determineMimeTypesByFilename(String filename) {
    String fileExtension = getExtension(filename);

    // try detecting the mimetype based on the web app logic equivalent
    List<String> mimeTypeList = FILE_EXTENSION_TO_MIMETYPE_MAPPING.get(fileExtension);
    if (mimeTypeList != null && mimeTypeList.size() > 0) {
        return mimeTypeList;
    } else {
        // try detecting the mime type via android itself
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
        if (mimeType != null) {
            return Collections.singletonList(mimeType);
        } else {
            return new ArrayList<>();
        }
    }
}

From source file:com.orange.ocara.ui.activity.ListAuditActivity.java

@Receiver(actions = AuditExportService.EXPORT_SUCCESS, local = true, registerAt = Receiver.RegisterAt.OnResumeOnPause)
@UiThread(propagation = UiThread.Propagation.REUSE)
void onExportAuditDone(@Receiver.Extra String path) {
    setLoading(false);/*www . j  a v  a 2s. c  o  m*/

    exportFile = new File(path);

    switch (exportAction) {
    case EXPORT_ACTION_SHARE: {

        // create an intent, so the user can choose which application he/she wants to use to share this file
        final Intent intent = ShareCompat.IntentBuilder.from(this)
                .setType(MimeTypeMap.getSingleton()
                        .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(path)))
                .setStream(FileProvider.getUriForFile(this, "com.orange.ocara", exportFile))
                .setChooserTitle("How do you want to share?").createChooserIntent()
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivity(intent);
        break;
    }

    case EXPORT_ACTION_SAVE: {
        createNewDocument(path);
        break;
    }
    }
}

From source file:com.owncloud.android.utils.MimeTypeUtil.java

public static String getMimeTypeFromPath(String path) {
    String extension = "";
    int pos = path.lastIndexOf('.');
    if (pos >= 0) {
        extension = path.substring(pos + 1);
    }//from w  w  w . j  av  a  2  s. c om
    String result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
    return (result != null) ? result : "";
}

From source file:com.raspi.chatapp.util.storage.file.LocalStorageProvider.java

@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    if (LocalStorageProvider.isMissingPermission(getContext())) {
        return null;
    }/*from w  w w.ja  v  a2s  .  c  om*/
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}

From source file:nuclei.ui.share.PackageTargetManager.java

protected void onSetFileProvider(Context context, String packageName, String authority, Intent intent) {
    if (mUri != null || mFile != null) {
        Uri uri = mUri;//from w w w  .j  a  v  a2 s  .c  om
        String type = "*/*";
        if (mFile != null) {
            uri = FileProvider.getUriForFile(context, authority, mFile);
            final int lastDot = mFile.getName().lastIndexOf('.');
            if (lastDot >= 0) {
                String extension = mFile.getName().substring(lastDot + 1);
                String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                if (mimeType != null)
                    type = mimeType;
            }
        }
        intent.setDataAndType(intent.getData(), type);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        if (packageName != null) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    } else {
        intent.setType("text/plain");
    }
}

From source file:com.lovejoy777sarootool.rootool.preview.MimeTypes.java

public static String getMimeType(File file) {
    if (file.isDirectory()) {
        return null;
    }//www.j a v  a2 s . c  o  m

    String type = null;
    final String extension = FilenameUtils.getExtension(file.getName());

    if (extension != null && !extension.isEmpty()) {
        final String extensionLowerCase = extension.toLowerCase(Locale.getDefault());
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extensionLowerCase);
        if (type == null) {
            type = MIME_TYPES.get(extensionLowerCase);
        }
    }
    return type;
}

From source file:com.orange.ocara.ui.activity.ListAuditActivity.java

private void createNewDocument(String path) {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType(//from  w w w.  ja v a2  s. c  om
            MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(path)));
    startActivityForResult(intent, ACTION_CREATE_NEW_DOCUMENT);
}

From source file:org.odk.collect.android.http.HttpClientConnection.java

@Override
public @NonNull HttpPostResult uploadSubmissionFile(@NonNull List<File> fileList, @NonNull File submissionFile,
        @NonNull URI uri, @Nullable HttpCredentialsInterface credentials, @NonNull long contentLength)
        throws IOException {
    addCredentialsForHost(uri, credentials);
    clearCookieStore();/*ww w .  j a  va  2  s. co  m*/

    HttpClient httpclient = createHttpClient(UPLOAD_CONNECTION_TIMEOUT);

    // if https then enable preemptive basic auth...
    if (uri.getScheme().equals("https")) {
        enablePreemptiveBasicAuth(uri.getHost());
    }

    HttpPostResult postResult = null;

    boolean first = true;
    int fileIndex = 0;
    int lastFileIndex;
    while (fileIndex < fileList.size() || first) {
        lastFileIndex = fileIndex;
        first = false;

        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();

        long byteCount = 0L;

        // mime post
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        // add the submission file first...
        FileBody fb = new FileBody(submissionFile, ContentType.TEXT_XML);
        builder.addPart("xml_submission_file", fb);
        Timber.i("added xml_submission_file: %s", submissionFile.getName());
        byteCount += submissionFile.length();

        for (; fileIndex < fileList.size(); fileIndex++) {
            File file = fileList.get(fileIndex);

            // we will be processing every one of these, so
            // we only need to deal with the content type determination...
            ContentType contentType = ContentTypeMapping.of(file.getName());
            if (contentType == null) {
                String mime = mimeTypeMap.getMimeTypeFromExtension(FileUtils.getFileExtension(file.getName()));
                if (mime != null) {
                    contentType = ContentType.create(mime);
                } else {
                    Timber.w("No specific MIME type found for file: %s", file.getName());
                    contentType = ContentType.APPLICATION_OCTET_STREAM;
                }
            }
            fb = new FileBody(file, contentType);
            builder.addPart(file.getName(), fb);
            byteCount += file.length();
            Timber.i("added file of type '%s' %s", contentType, file.getName());

            // we've added at least one attachment to the request...
            if (fileIndex + 1 < fileList.size()) {
                if ((fileIndex - lastFileIndex + 1 > 100)
                        || (byteCount + fileList.get(fileIndex + 1).length() > contentLength)) {
                    // the next file would exceed the 10MB threshold...
                    Timber.i("Extremely long post is being split into multiple posts");
                    try {
                        StringBody sb = new StringBody("yes",
                                ContentType.TEXT_PLAIN.withCharset(Charset.forName("UTF-8")));
                        builder.addPart("*isIncomplete*", sb);
                    } catch (Exception e) {
                        Timber.e(e);
                    }
                    ++fileIndex; // advance over the last attachment added...
                    break;
                }
            }
        }

        HttpPost httppost = createOpenRosaHttpPost(uri);
        httppost.setEntity(builder.build());

        // prepare response and return uploaded
        HttpResponse response;

        try {
            Timber.i("Issuing POST request to: %s", uri.toString());
            response = httpclient.execute(httppost, httpContext);
            int responseCode = response.getStatusLine().getStatusCode();
            HttpEntity httpEntity = response.getEntity();
            Timber.i("Response code:%d", responseCode);

            postResult = new HttpPostResult(EntityUtils.toString(httpEntity), responseCode,
                    response.getStatusLine().getReasonPhrase());

            discardEntityBytes(response);

            if (responseCode == HttpStatus.SC_UNAUTHORIZED) {
                clearCookieStore();
            }

            if (responseCode != HttpStatus.SC_CREATED && responseCode != HttpStatus.SC_ACCEPTED) {
                return postResult;
            }

        } catch (IOException e) {
            if (e instanceof UnknownHostException || e instanceof HttpHostConnectException
                    || e instanceof SocketException || e instanceof NoHttpResponseException
                    || e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException) {
                Timber.i(e);
            } else {
                Timber.e(e);
            }
            String msg = e.getMessage();
            if (msg == null) {
                msg = e.toString();
            }

            throw new IOException(msg);
        }
    }

    return postResult;
}