open File via Uri with mimetype - Android android.webkit

Android examples for android.webkit:MimeTypeMap

Description

open File via Uri with mimetype

Demo Code

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.webkit.MimeTypeMap;

public class Main {

  public static void openFile(Context context, Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String type = "*/*";

    String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
    type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

    intent.setDataAndType(uri, type);//from w w w .  j a va  2s.  c  o m

    try {
      context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
      e.printStackTrace();
      UIUtils.gotoBrowserActivity(context, uri.toString());
    }
  }

}

Related Tutorials