import android.content.ActivityNotFoundException;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.widget.Toast;import com.xuehu365.xuehu.R;import com.xuehu365.xuehu.ui.widget.AlertDialog;import java.io.File;/** * Created by David on 2016/8/17. */public class FileOpenIntentHelp { //android获取一个用于打开文件的intent public static Intent getFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "*/*"); return intent; } //android获取一个用于打开Word文件的intent public static Intent getWordFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/msword"); return intent; } //android获取一个用于打开文本文件的intent public static Intent getTextFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "text/plain"); return intent; } //android获取一个用于打开PDF文件的intent public static Intent getPdfFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/pdf"); return intent; } //Android获取一个用于打开PPT文件的intent public static Intent getPptFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); return intent; } //Android获取一个用于打开Excel文件的intent public static Intent getExcelFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/vnd.ms-excel"); return intent; } /** * 使用intent打开文件 * * @param fileFullName * @param fileExt * @param context */ public static void openFile(String fileFullName, String fileExt, Context context) { try { if (FileMIMEHelp.checkEndsWithInStringArray(fileFullName, context.getResources().getStringArray(R.array.fileEndingPdf))) { Intent fileIntent = FileOpenIntentHelp.getPdfFileIntent(fileFullName); context.startActivity(fileIntent); } else if (FileMIMEHelp.checkEndsWithInStringArray(fileFullName, context.getResources().getStringArray(R.array.fileEndingText))) { Intent fileIntent = FileOpenIntentHelp.getTextFileIntent(fileFullName); context.startActivity(fileIntent); } else if (FileMIMEHelp.checkEndsWithInStringArray(fileFullName, context.getResources().getStringArray(R.array.fileEndingPPT))) { Intent fileIntent = FileOpenIntentHelp.getPptFileIntent(fileFullName); context.startActivity(fileIntent); } else if (FileMIMEHelp.checkEndsWithInStringArray(fileFullName, context.getResources().getStringArray(R.array.fileEndingWord))) { Intent fileIntent = FileOpenIntentHelp.getWordFileIntent(fileFullName); context.startActivity(fileIntent); } else if (FileMIMEHelp.checkEndsWithInStringArray(fileFullName, context.getResources().getStringArray(R.array.fileEndingExcel))) { Intent fileIntent = FileOpenIntentHelp.getExcelFileIntent(fileFullName); context.startActivity(fileIntent); } else { Intent fileIntent = FileOpenIntentHelp.getFileIntent(fileFullName); context.startActivity(fileIntent); } } catch (NullPointerException e) { new AlertDialog(context).builder().setTitle("文件损坏").setMsg("文件不存在或文件已损坏!").setPositiveButton("好", null).show(); } catch (ActivityNotFoundException e) { Toast.makeText(context, "找不到合适的应用打开该文件。请安装合适应用", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(context, "打开文件发生错误", Toast.LENGTH_SHORT).show(); } } /** * 文件是否存在 * * @param fileFullName * @return */ public static boolean isFileExist(String fileFullName) { boolean b = false; try { File file = new File(fileFullName); if (file.exists()) { b = true; } } catch (Exception e) { b = false; } return b; }}