|
1 | 1 | package com.jingewenku.abrahamcaijin.commonutil;
|
2 | 2 |
|
| 3 | +import android.app.DownloadManager; |
3 | 4 | import android.content.Context;
|
| 5 | +import android.content.Intent; |
4 | 6 | import android.content.SharedPreferences;
|
5 | 7 | import android.graphics.Bitmap;
|
| 8 | +import android.net.Uri; |
6 | 9 | import android.os.Environment;
|
7 | 10 | import android.os.StatFs;
|
| 11 | +import android.text.format.Formatter; |
8 | 12 | import android.util.Log;
|
9 | 13 | import com.jingewenku.abrahamcaijin.commonutil.klog.KLog;
|
10 | 14 |
|
11 | 15 | import java.io.*;
|
| 16 | +import java.nio.channels.FileChannel; |
12 | 17 | import java.util.ArrayList;
|
13 | 18 | import java.util.Iterator;
|
14 | 19 | import java.util.List;
|
15 | 20 | import java.util.Map;
|
| 21 | +import java.util.zip.GZIPInputStream; |
| 22 | +import java.util.zip.GZIPOutputStream; |
| 23 | + |
| 24 | +import static com.jingewenku.abrahamcaijin.commonutil.AppFileMgr.closeIO; |
| 25 | +import static com.jingewenku.abrahamcaijin.commonutil.AppValidationMgr.convertStreamToString; |
16 | 26 |
|
17 | 27 | /**
|
18 | 28 | * @Description:主要功能:文件管理
|
@@ -825,4 +835,302 @@ public static void saveAsPNG(Bitmap bitmap, String filePath)
|
825 | 835 | }
|
826 | 836 | }
|
827 | 837 |
|
| 838 | + /** |
| 839 | + * 将文件转成字符串 |
| 840 | + * |
| 841 | + * @param file |
| 842 | + * 文件 |
| 843 | + * @return |
| 844 | + * @throws Exception |
| 845 | + */ |
| 846 | + public static String getStringFromFile(File file) throws Exception { |
| 847 | + FileInputStream fin = new FileInputStream(file); |
| 848 | + String ret = convertStreamToString(fin); |
| 849 | + // Make sure you close all streams. |
| 850 | + fin.close(); |
| 851 | + return ret; |
| 852 | + } |
| 853 | + |
| 854 | + /** |
| 855 | + * 复制文件 |
| 856 | + * @param in |
| 857 | + * @param out |
| 858 | + */ |
| 859 | + public static void copyFile(InputStream in, OutputStream out) { |
| 860 | + try { |
| 861 | + byte[] b = new byte[2 * 1024 * 1024]; //2M memory |
| 862 | + int len = -1; |
| 863 | + while ((len = in.read(b)) > 0) { |
| 864 | + out.write(b, 0, len); |
| 865 | + out.flush(); |
| 866 | + } |
| 867 | + } catch (IOException e) { |
| 868 | + e.printStackTrace(); |
| 869 | + } finally { |
| 870 | + closeIO(in, out); |
| 871 | + } |
| 872 | + } |
| 873 | + |
| 874 | + /** |
| 875 | + *快速复制 |
| 876 | + * @param in |
| 877 | + * @param out |
| 878 | + */ |
| 879 | + public static void copyFileFast(File in, File out) { |
| 880 | + FileChannel filein = null; |
| 881 | + FileChannel fileout = null; |
| 882 | + try { |
| 883 | + filein = new FileInputStream(in).getChannel(); |
| 884 | + fileout = new FileOutputStream(out).getChannel(); |
| 885 | + filein.transferTo(0, filein.size(), fileout); |
| 886 | + } catch (FileNotFoundException e) { |
| 887 | + e.printStackTrace(); |
| 888 | + } catch (IOException e) { |
| 889 | + e.printStackTrace(); |
| 890 | + } finally { |
| 891 | + closeIO(filein, fileout); |
| 892 | + } |
| 893 | + } |
| 894 | + |
| 895 | + /** |
| 896 | + *分享文件 |
| 897 | + * @param context |
| 898 | + * @param title |
| 899 | + * @param filePath |
| 900 | + */ |
| 901 | + public static void shareFile(Context context, String title, String filePath) { |
| 902 | + Intent intent = new Intent(Intent.ACTION_SEND); |
| 903 | + Uri uri = Uri.parse("file://" + filePath); |
| 904 | + intent.setType("*/*"); |
| 905 | + intent.putExtra(Intent.EXTRA_STREAM, uri); |
| 906 | + context.startActivity(Intent.createChooser(intent, title)); |
| 907 | + } |
| 908 | + |
| 909 | + /** |
| 910 | + *压缩 |
| 911 | + * @param is |
| 912 | + * @param os |
| 913 | + */ |
| 914 | + public static void zip(InputStream is, OutputStream os) { |
| 915 | + GZIPOutputStream gzip = null; |
| 916 | + try { |
| 917 | + gzip = new GZIPOutputStream(os); |
| 918 | + byte[] buf = new byte[1024]; |
| 919 | + int len; |
| 920 | + while ((len = is.read(buf)) != -1) { |
| 921 | + gzip.write(buf, 0, len); |
| 922 | + gzip.flush(); |
| 923 | + } |
| 924 | + } catch (IOException e) { |
| 925 | + e.printStackTrace(); |
| 926 | + } finally { |
| 927 | + closeIO(is, gzip); |
| 928 | + } |
| 929 | + } |
| 930 | + |
| 931 | + /** |
| 932 | + *解压 |
| 933 | + * @param is |
| 934 | + * @param os |
| 935 | + */ |
| 936 | + public static void unzip(InputStream is, OutputStream os) { |
| 937 | + GZIPInputStream gzip = null; |
| 938 | + try { |
| 939 | + gzip = new GZIPInputStream(is); |
| 940 | + byte[] buf = new byte[1024]; |
| 941 | + int len; |
| 942 | + while ((len = gzip.read(buf)) != -1) { |
| 943 | + os.write(buf, 0, len); |
| 944 | + } |
| 945 | + } catch (IOException e) { |
| 946 | + e.printStackTrace(); |
| 947 | + } finally { |
| 948 | + closeIO(gzip, os); |
| 949 | + } |
| 950 | + } |
| 951 | + |
| 952 | + /** |
| 953 | + * 格式化文件大小 |
| 954 | + * @param context |
| 955 | + * @param size |
| 956 | + * @return |
| 957 | + */ |
| 958 | + public static String formatFileSize(Context context, long size) { |
| 959 | + return Formatter.formatFileSize(context, size); |
| 960 | + } |
| 961 | + |
| 962 | + /** |
| 963 | + * 将输入流写入到文件 |
| 964 | + * @param is |
| 965 | + * @param file |
| 966 | + */ |
| 967 | + public static void Stream2File(InputStream is, File file) { |
| 968 | + byte[] b = new byte[1024]; |
| 969 | + int len; |
| 970 | + FileOutputStream os = null; |
| 971 | + try { |
| 972 | + os = new FileOutputStream(file); |
| 973 | + while ((len = is.read(b)) != -1) { |
| 974 | + os.write(b, 0, len); |
| 975 | + os.flush(); |
| 976 | + } |
| 977 | + } catch (IOException e) { |
| 978 | + e.printStackTrace(); |
| 979 | + } finally { |
| 980 | + closeIO(is, os); |
| 981 | + } |
| 982 | + } |
| 983 | + |
| 984 | + /** |
| 985 | + * 创建文件夹(支持覆盖已存在的同名文件夹) |
| 986 | + * @param filePath |
| 987 | + * @param recreate |
| 988 | + * @return |
| 989 | + */ |
| 990 | + public static boolean createFolder(String filePath, boolean recreate) { |
| 991 | + String folderName = getFolderName(filePath); |
| 992 | + if (folderName == null || folderName.length() == 0 || folderName.trim().length() == 0) { |
| 993 | + return false; |
| 994 | + } |
| 995 | + File folder = new File(folderName); |
| 996 | + if (folder.exists()) { |
| 997 | + if (recreate) { |
| 998 | + deleteFile(folderName); |
| 999 | + return folder.mkdirs(); |
| 1000 | + } else { |
| 1001 | + return true; |
| 1002 | + } |
| 1003 | + } else { |
| 1004 | + return folder.mkdirs(); |
| 1005 | + } |
| 1006 | + } |
| 1007 | + |
| 1008 | + public static boolean deleteFile(String filename) { |
| 1009 | + return new File(filename).delete(); |
| 1010 | + } |
| 1011 | + |
| 1012 | + /** |
| 1013 | + * 获取文件名 |
| 1014 | + * @param filePath |
| 1015 | + * @return |
| 1016 | + */ |
| 1017 | + public static String getFileName(String filePath) { |
| 1018 | + if (AppStringUtils.isEmpty(filePath)) { |
| 1019 | + return filePath; |
| 1020 | + } |
| 1021 | + |
| 1022 | + int filePosi = filePath.lastIndexOf(File.separator); |
| 1023 | + return (filePosi == -1) ? filePath : filePath.substring(filePosi + 1); |
| 1024 | + } |
| 1025 | + |
| 1026 | + /** |
| 1027 | + * 重命名文件\文件夹 |
| 1028 | + * @param filepath |
| 1029 | + * @param newName |
| 1030 | + * @return |
| 1031 | + */ |
| 1032 | + public static boolean rename(String filepath, String newName) { |
| 1033 | + File file = new File(filepath); |
| 1034 | + return file.exists() && file.renameTo(new File(newName)); |
| 1035 | + } |
| 1036 | + |
| 1037 | + /** |
| 1038 | + * 获取文件夹名称 |
| 1039 | + * @param filePath |
| 1040 | + * @return |
| 1041 | + */ |
| 1042 | + public static String getFolderName(String filePath) { |
| 1043 | + if (filePath == null || filePath.length() == 0 || filePath.trim().length() == 0) { |
| 1044 | + return filePath; |
| 1045 | + } |
| 1046 | + int filePos = filePath.lastIndexOf(File.separator); |
| 1047 | + return (filePos == -1) ? "" : filePath.substring(0, filePos); |
| 1048 | + } |
| 1049 | + |
| 1050 | + /** |
| 1051 | + * 获取文件夹下所有文件 |
| 1052 | + * @param path |
| 1053 | + * @return |
| 1054 | + */ |
| 1055 | + public static ArrayList<File> getFilesArray(String path) { |
| 1056 | + File file = new File(path); |
| 1057 | + File files[] = file.listFiles(); |
| 1058 | + ArrayList<File> listFile = new ArrayList<File>(); |
| 1059 | + if (files != null) { |
| 1060 | + for (int i = 0; i < files.length; i++) { |
| 1061 | + if (files[i].isFile()) { |
| 1062 | + listFile.add(files[i]); |
| 1063 | + } |
| 1064 | + if (files[i].isDirectory()) { |
| 1065 | + listFile.addAll(getFilesArray(files[i].toString())); |
| 1066 | + } |
| 1067 | + } |
| 1068 | + } |
| 1069 | + return listFile; |
| 1070 | + } |
| 1071 | + |
| 1072 | + /** |
| 1073 | + * 打开图片 |
| 1074 | + * @param mContext |
| 1075 | + * @param imagePath |
| 1076 | + */ |
| 1077 | + public static void openImage(Context mContext, String imagePath) { |
| 1078 | + Intent intent = new Intent("android.intent.action.VIEW"); |
| 1079 | + intent.addCategory("android.intent.category.DEFAULT"); |
| 1080 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1081 | + Uri uri = Uri.fromFile(new File(imagePath)); |
| 1082 | + intent.setDataAndType(uri, "image/*"); |
| 1083 | + mContext.startActivity(intent); |
| 1084 | + } |
| 1085 | + |
| 1086 | + /** |
| 1087 | + * 打开视频 |
| 1088 | + * @param mContext |
| 1089 | + * @param videoPath |
| 1090 | + */ |
| 1091 | + public static void openVideo(Context mContext, String videoPath) { |
| 1092 | + Intent intent = new Intent("android.intent.action.VIEW"); |
| 1093 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 1094 | + intent.putExtra("oneshot", 0); |
| 1095 | + intent.putExtra("configchange", 0); |
| 1096 | + Uri uri = Uri.fromFile(new File(videoPath)); |
| 1097 | + intent.setDataAndType(uri, "video/*"); |
| 1098 | + mContext.startActivity(intent); |
| 1099 | + } |
| 1100 | + |
| 1101 | + /** |
| 1102 | + * 打开URL |
| 1103 | + * @param mContext |
| 1104 | + * @param url |
| 1105 | + */ |
| 1106 | + public static void openURL(Context mContext, String url) { |
| 1107 | + Uri uri = Uri.parse(url); |
| 1108 | + Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
| 1109 | + mContext.startActivity(intent); |
| 1110 | + } |
| 1111 | + |
| 1112 | + /** |
| 1113 | + * 下载文件 |
| 1114 | + * @param context |
| 1115 | + * @param fileurl |
| 1116 | + */ |
| 1117 | + public static void downloadFile(Context context, String fileurl) { |
| 1118 | + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileurl)); |
| 1119 | + request.setDestinationInExternalPublicDir("/Download/", fileurl.substring(fileurl.lastIndexOf("/") + 1)); |
| 1120 | + DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
| 1121 | + downloadManager.enqueue(request); |
| 1122 | + } |
| 1123 | + |
| 1124 | + /** |
| 1125 | + * 通过APKURL升级应用 |
| 1126 | + * @param context |
| 1127 | + * @param fileurl |
| 1128 | + */ |
| 1129 | + public static void upgradeApp(Context context, String fileurl) { |
| 1130 | + Intent intent = new Intent(context, DownloadService.class); |
| 1131 | + intent.putExtra("fileurl", fileurl); |
| 1132 | + context.startService(intent); |
| 1133 | + } |
| 1134 | + |
| 1135 | + |
828 | 1136 | }
|
0 commit comments