Android examples for Intent:Open File
open File External via Intent
//package com.book2s; import java.io.File; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.text.TextUtils; import android.webkit.MimeTypeMap; public class Main { public static void openFileExternal(Context context, String filePath) { if (TextUtils.isEmpty(filePath)) { return; }//from w w w.j a v a2 s . c om Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); String type = getMimeType(filePath); intent.setDataAndType(Uri.fromFile(new File(filePath)), type); try { context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } public static String getMimeType(String filePath) { String type = null; String extension = getSuffix(filePath); if (!TextUtils.isEmpty(extension)) { MimeTypeMap mime = MimeTypeMap.getSingleton(); type = mime.getMimeTypeFromExtension(extension); } if (TextUtils.isEmpty(type)) { type = "file/*"; } return type; } public static String getSuffix(String filePath) { if (TextUtils.isEmpty(filePath)) { return null; } int dotPos = filePath.lastIndexOf('.'); if (dotPos >= 0 && dotPos < filePath.length() - 1) { return filePath.substring(dotPos + 1); } return null; } }