Java Code Examples for android.content.Intent.normalizeMimeType()

The following are Jave code examples for showing how to use normalizeMimeType() of the android.content.Intent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: chromium-for-android-56-debug-video   File: DownloadUtils.java   View Source Code Vote up 6 votes
/**
 * Creates an Intent to open the file in another app by firing an Intent to Android.
 * @param fileUri  Uri pointing to the file.
 * @param mimeType MIME type for the file.
 * @return Intent that can be used to start an Activity for the file.
 */
public static Intent createViewIntentForDownloadItem(Uri fileUri, String mimeType) {
    Intent fileIntent = new Intent(Intent.ACTION_VIEW);
    String normalizedMimeType = Intent.normalizeMimeType(mimeType);
    if (TextUtils.isEmpty(normalizedMimeType)) {
        fileIntent.setData(fileUri);
    } else {
        fileIntent.setDataAndType(fileUri, normalizedMimeType);
    }
    fileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    fileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return fileIntent;
}
 
Example 2
Project: chromium-for-android-56-debug-video   File: DownloadUtils.java   View Source Code Vote up 6 votes
/**
 * Opens a file in Chrome or in another app if appropriate.
 * @param file path to the file to open.
 * @param mimeType mime type of the file.
 * @param isOffTheRecord whether we are in an off the record context.
 * @return whether the file could successfully be opened.
 */
public static boolean openFile(File file, String mimeType, boolean isOffTheRecord) {
    Context context = ContextUtils.getApplicationContext();
    Intent viewIntent = createViewIntentForDownloadItem(Uri.fromFile(file), mimeType);
    DownloadManagerService service = DownloadManagerService.getDownloadManagerService(context);

    // Check if Chrome should open the file itself.
    if (service.isDownloadOpenableInBrowser(isOffTheRecord, mimeType)) {
        // Share URIs use the content:// scheme when able, which looks bad when displayed
        // in the URL bar.
        Uri fileUri = Uri.fromFile(file);
        Uri shareUri = getUriForItem(file);
        String normalizedMimeType = Intent.normalizeMimeType(mimeType);

        Intent intent =
                getMediaViewerIntentForDownloadItem(fileUri, shareUri, normalizedMimeType);
        IntentHandler.startActivityForTrustedIntent(intent, context);
        return true;
    }

    // Check if any apps can open the file.
    try {
        context.startActivity(viewIntent);
        return true;
    } catch (ActivityNotFoundException e) {
        // Can't launch the Intent.
        Toast.makeText(context, context.getString(R.string.download_cant_open_file),
                     Toast.LENGTH_SHORT)
                .show();
        return false;
    }
}