android各种格式文件的打开(代码)

public Intent getIntent(File file, String str) {
    Intent intent;
    Uri uri;
    intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    switch (str) {
        case "ppt":

            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
            break;
        case "xls":
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/vnd.ms-excel");
            break;
        case "doc":
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/msword");
            break;
        case "chm":
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/x-chm");
            break;
        case "txt":
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "text/plain");
            break;
        case "pdf":
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            break;
        case "PNG":
        case "jpg":
        case "png":
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "image/*");
            break;
        default:
            uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/msword");
            break;
    }
    return intent;
}

你可能感兴趣的:(noob)