安卓打开各种文件工具类FileUtils


打开ZIP,RAR、APK、PPT、JPG、JIF、PNG、MP4、DOC、DOCX等等 

package com.youli.zbetuch_huangpu.utils;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import java.io.File;
import java.util.List;

/**
 * Created by liutao on 2017/9/1.
 */

public class FileUtils {

    public static Intent openFile(String filePath) {

        File file = new File(filePath);

        if ((file == null) || !file.exists() || file.isDirectory())
            return null;

		/* 取得扩展名 */
        String end = file
                .getName()
                .substring(file.getName().lastIndexOf(".") + 1,
                        file.getName().length()).toLowerCase();
		/* 依扩展名的类型决定MimeType */
        if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
                || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {

            return getAudioFileIntent(filePath);
        } else if (end.equals("3gp") || end.equals("mp4")) {

            return getAudioFileIntent(filePath);
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
                || end.equals("jpeg") || end.equals("bmp")) {

            return getImageFileIntent(filePath);
        } else if (end.equals("apk")) {

            return getApkFileIntent(filePath);
        } else if (end.equals("ppt")) {

            return getPptFileIntent(filePath);
        } else if (end.equals("xls")) {

            return getExcelFileIntent(filePath);
        } else if (end.equals("doc") || end.equals("docx")) {

            return getWordFileIntent(filePath);

        } else if (end.equals("pdf")) {

            return getPdfFileIntent(filePath);
        } else if (end.equals("chm")) {

            return getChmFileIntent(filePath);
        } else if (end.equals("txt")||end.equals("rar")) {

            return getTextFileIntent(filePath, false);
        } else if (end.equals("zip")||end.equals("rar")){

            return getZIPFileIntent(filePath);
        }else {
            return getAllIntent(end,filePath);
        }
    }


    // Android获取一个用于打开APK文件的intent
    public static Intent getAllIntent(String end,String param) {

        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT  packageInfoList = packageManager
                    .getInstalledPackages(0);
            for (PackageInfo info : packageInfoList) {
                // 判断如果不是系统apk
                if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
                    // System.out.println(info.applicationInfo.packageName
                    // + "===>"
                    // + packageManager
                    // .getApplicationLabel(info.applicationInfo));
                    value += packageManager
                            .getApplicationLabel(info.applicationInfo) + ",";
                }

                // 获得应用的图标
                // packageManager.getApplicationIcon(applicationInfo)
            }
            if (value.trim().length() > 1) {
                value = value.substring(0, value.length() - 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    public static File getSaveFile(Context context) {
        return new File(context.getFilesDir(), "pic.jpg");
    }

}

你可能感兴趣的:(工具类,android入门教学)