• android获取一个用于打开Word文件的intent


     通过Intent打开手机中能够阅读word,excle或pdf的应用,让那个应用来打开文件。这里给出word,excle的open

    OpenWord类

    package com.example.tomorrow.MobileOA.utils;
    
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.pm.ResolveInfo;
    import android.net.Uri;
    
    import java.io.File;
    import java.util.List;
    
    /**
     * Created by he on 11/10 2015.
    * wang */ public class OpenWord {   //param为文档的主要路径 public static Intent getWordFileIntent(String param) { Intent intent = null; try { 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"); } catch (Exception e) { e.printStackTrace(); } return intent; } public static Intent getExcelFileIntent(File 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(param); intent.setDataAndType(uri, "application/vnd.ms-excel"); return intent; } /** * 判断Intent 是否存在 防止崩溃 * @param context * @param intent * @return */ public static boolean isIntentAvailable(Context context, Intent intent) { final PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES); return list.size() > 0; } }

     为一个按钮绑定事件

    bt_download_look.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = files.get(position).getName();
                    FileUtils fileUtils = new FileUtils();
                    String path = fileUtils.getSDPATH() + "MobileOA/" + name;
                    Intent intent = OpenWord.getWordFileIntent(path);
                    boolean flag = OpenWord.isIntentAvailable(context, intent);
                    if (!flag) {
                        Toast.makeText(context, "请安装offer", Toast.LENGTH_SHORT).show();
                    } else {
                        context.startActivity(intent);
                    }
                }
            });
    
  • 相关阅读:
    python 上传下载文件
    post方式加载iframe
    js 实现打印功能
    python 判断数据类型
    web样式无法正常显示
    C# 调用python
    PDF转换成Txt
    js预览PDF的插件(亲测支持IE9,火狐,等等)
    文件下载
    asp.net网站发布到服务器GET的技能
  • 原文地址:https://www.cnblogs.com/wabi87547568/p/4966712.html
Copyright © 2020-2023  润新知