first
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.ckkj.water.utils.dataUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 小数计算
|
||||
*/
|
||||
public class DoubleTrans {
|
||||
public static String doubleTrans(double d){
|
||||
if(Math.round(d)-d==0){
|
||||
return String.valueOf((long)d);
|
||||
}
|
||||
return String.valueOf(d);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保留两位小数,并四舍五入
|
||||
*/
|
||||
public static double fromatDoubleTwo(double Num) {
|
||||
BigDecimal bg = new BigDecimal(String.valueOf(Num));
|
||||
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||||
return f1;
|
||||
}
|
||||
|
||||
public static String fromatDoubleTwoStr(String Num) {
|
||||
BigDecimal bg = new BigDecimal(Num);
|
||||
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||||
return String.format("%.2f", f1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留两位小数,并四舍五入
|
||||
*/
|
||||
public static String fromatDoubleTwoStr(double Num) {
|
||||
BigDecimal bg = new BigDecimal(Num);
|
||||
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||||
return String.format("%.2f", f1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留两位小数,并四舍五入
|
||||
*/
|
||||
public static String fromatDoubleTwoStr(BigDecimal Num) {
|
||||
double f1 = Num.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||||
return String.format("%.2f", f1);
|
||||
}
|
||||
|
||||
public static String getCountVipPrice(String marketPrice,double userDiscount){
|
||||
BigDecimal bgMark = new BigDecimal(marketPrice);
|
||||
BigDecimal bgDis = new BigDecimal(String.valueOf(userDiscount));
|
||||
BigDecimal bgVipPirce = bgMark.multiply(bgDis);
|
||||
String countPriceStr = fromatDoubleTwoStr(bgVipPirce);
|
||||
// double doublePrice = Double.parseDouble(marketPrice);
|
||||
// double vipPrice = doublePrice * userDiscount;
|
||||
// String countPriceStr = fromatDoubleTwoStr(fromatDoubleTwo(vipPrice));
|
||||
return countPriceStr;
|
||||
}
|
||||
|
||||
public static String getCountVipPriceInt(String marketPrice,int userDiscount){
|
||||
double doublePrice = Double.parseDouble(marketPrice);
|
||||
double vipPrice = doublePrice * userDiscount;
|
||||
String countPriceStr = fromatDoubleTwoStr(fromatDoubleTwo(vipPrice));
|
||||
return countPriceStr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,534 @@
|
||||
package com.ckkj.water.utils.dataUtil;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by dty on 2015/11/9.
|
||||
* 指定文件的打开方式,可直接打开
|
||||
*/
|
||||
public class FileOpenWays {
|
||||
/**
|
||||
* -- MIME 列表 --
|
||||
*/
|
||||
public static final String[][] MIME_MapTable =
|
||||
{
|
||||
// --{后缀名, MIME类型} --
|
||||
{".3gp", "video/3gpp"},
|
||||
{".3gpp", "video/3gpp"},
|
||||
{".aac", "audio/x-mpeg"},
|
||||
{".amr", "audio/x-mpeg"},
|
||||
{".apk", "application/vnd.android.package-archive"},
|
||||
{".avi", "video/x-msvideo"},
|
||||
{".aab", "application/x-authoware-bin"},
|
||||
{".aam", "application/x-authoware-map"},
|
||||
{".aas", "application/x-authoware-seg"},
|
||||
{".ai", "application/postscript"},
|
||||
{".aif", "audio/x-aiff"},
|
||||
{".aifc", "audio/x-aiff"},
|
||||
{".aiff", "audio/x-aiff"},
|
||||
{".als", "audio/x-alpha5"},
|
||||
{".amc", "application/x-mpeg"},
|
||||
{".ani", "application/octet-stream"},
|
||||
{".asc", "text/plain"},
|
||||
{".asd", "application/astound"},
|
||||
{".asf", "video/x-ms-asf"},
|
||||
{".asn", "application/astound"},
|
||||
{".asp", "application/x-asap"},
|
||||
{".asx", " video/x-ms-asf"},
|
||||
{".au", "audio/basic"},
|
||||
{".avb", "application/octet-stream"},
|
||||
{".awb", "audio/amr-wb"},
|
||||
{".bcpio", "application/x-bcpio"},
|
||||
{".bld", "application/bld"},
|
||||
{".bld2", "application/bld2"},
|
||||
{".bpk", "application/octet-stream"},
|
||||
{".bz2", "application/x-bzip2"},
|
||||
{".bin", "application/octet-stream"},
|
||||
{".bmp", "image/bmp"},
|
||||
{".c", "text/plain"},
|
||||
{".class", "application/octet-stream"},
|
||||
{".conf", "text/plain"},
|
||||
{".cpp", "text/plain"},
|
||||
{".cal", "image/x-cals"},
|
||||
{".ccn", "application/x-cnc"},
|
||||
{".cco", "application/x-cocoa"},
|
||||
{".cdf", "application/x-netcdf"},
|
||||
{".cgi", "magnus-internal/cgi"},
|
||||
{".chat", "application/x-chat"},
|
||||
{".clp", "application/x-msclip"},
|
||||
{".cmx", "application/x-cmx"},
|
||||
{".co", "application/x-cult3d-object"},
|
||||
{".cod", "image/cis-cod"},
|
||||
{".cpio", "application/x-cpio"},
|
||||
{".cpt", "application/mac-compactpro"},
|
||||
{".crd", "application/x-mscardfile"},
|
||||
{".csh", "application/x-csh"},
|
||||
{".csm", "chemical/x-csml"},
|
||||
{".csml", "chemical/x-csml"},
|
||||
{".css", "text/css"},
|
||||
{".cur", "application/octet-stream"},
|
||||
{".doc", "application/msword"},
|
||||
{".dcm", "x-lml/x-evm"},
|
||||
{".dcr", "application/x-director"},
|
||||
{".dcx", "image/x-dcx"},
|
||||
{".dhtml", "text/html"},
|
||||
{".dir", "application/x-director"},
|
||||
{".dll", "application/octet-stream"},
|
||||
{".dmg", "application/octet-stream"},
|
||||
{".dms", "application/octet-stream"},
|
||||
{".dot", "application/x-dot"},
|
||||
{".dvi", "application/x-dvi"},
|
||||
{".dwf", "drawing/x-dwf"},
|
||||
{".dwg", "application/x-autocad"},
|
||||
{".dxf", "application/x-autocad"},
|
||||
{".dxr", "application/x-director"},
|
||||
{".ebk", "application/x-expandedbook"},
|
||||
{".emb", "chemical/x-embl-dl-nucleotide"},
|
||||
{".embl", "chemical/x-embl-dl-nucleotide"},
|
||||
{".eps", "application/postscript"},
|
||||
{".epub", "application/epub+zip"},
|
||||
{".eri", "image/x-eri"},
|
||||
{".es", "audio/echospeech"},
|
||||
{".esl", "audio/echospeech"},
|
||||
{".etc", "application/x-earthtime"},
|
||||
{".etx", "text/x-setext"},
|
||||
{".evm", "x-lml/x-evm"},
|
||||
{".evy", "application/x-envoy"},
|
||||
{".exe", "application/octet-stream"},
|
||||
{".fh4", "image/x-freehand"},
|
||||
{".fh5", "image/x-freehand"},
|
||||
{".fhc", "image/x-freehand"},
|
||||
{".fif", "image/fif"},
|
||||
{".fm", "application/x-maker"},
|
||||
{".fpx", "image/x-fpx"},
|
||||
{".fvi", "video/isivideo"},
|
||||
{".flv", "video/x-msvideo"},
|
||||
{".gau", "chemical/x-gaussian-input"},
|
||||
{".gca", "application/x-gca-compressed"},
|
||||
{".gdb", "x-lml/x-gdb"},
|
||||
{".gif", "image/gif"},
|
||||
{".gps", "application/x-gps"},
|
||||
{".gtar", "application/x-gtar"},
|
||||
{".gz", "application/x-gzip"},
|
||||
{".gif", "image/gif"},
|
||||
{".gtar", "application/x-gtar"},
|
||||
{".gz", "application/x-gzip"},
|
||||
{".h", "text/plain"},
|
||||
{".hdf", "application/x-hdf"},
|
||||
{".hdm", "text/x-hdml"},
|
||||
{".hdml", "text/x-hdml"},
|
||||
{".htm", "text/html"},
|
||||
{".html", "text/html"},
|
||||
{".hlp", "application/winhlp"},
|
||||
{".hqx", "application/mac-binhex40"},
|
||||
{".hts", "text/html"},
|
||||
{".ice", "x-conference/x-cooltalk"},
|
||||
{".ico", "application/octet-stream"},
|
||||
{".ief", "image/ief"},
|
||||
{".ifm", "image/gif"},
|
||||
{".ifs", "image/ifs"},
|
||||
{".imy", "audio/melody"},
|
||||
{".ins", "application/x-net-install"},
|
||||
{".ips", "application/x-ipscript"},
|
||||
{".ipx", "application/x-ipix"},
|
||||
{".it", "audio/x-mod"},
|
||||
{".itz", "audio/x-mod"},
|
||||
{".ivr", "i-world/i-vrml"},
|
||||
{".j2k", "image/j2k"},
|
||||
{".jad", "text/vnd.sun.j2me.app-descriptor"},
|
||||
{".jam", "application/x-jam"},
|
||||
{".jnlp", "application/x-java-jnlp-file"},
|
||||
{".jpe", "image/jpeg"},
|
||||
{".jpz", "image/jpeg"},
|
||||
{".jwc", "application/jwc"},
|
||||
{".jar", "application/java-archive"},
|
||||
{".java", "text/plain"},
|
||||
{".jpeg", "image/jpeg"},
|
||||
{".jpg", "image/jpg"},
|
||||
{".js", "application/x-javascript"},
|
||||
{".kjx", "application/x-kjx"},
|
||||
{".lak", "x-lml/x-lak"},
|
||||
{".latex", "application/x-latex"},
|
||||
{".lcc", "application/fastman"},
|
||||
{".lcl", "application/x-digitalloca"},
|
||||
{".lcr", "application/x-digitalloca"},
|
||||
{".lgh", "application/lgh"},
|
||||
{".lha", "application/octet-stream"},
|
||||
{".lml", "x-lml/x-lml"},
|
||||
{".lmlpack", "x-lml/x-lmlpack"},
|
||||
{".log", "text/plain"},
|
||||
{".lsf", "video/x-ms-asf"},
|
||||
{".lsx", "video/x-ms-asf"},
|
||||
{".lzh", "application/x-lzh "},
|
||||
{".m13", "application/x-msmediaview"},
|
||||
{".m14", "application/x-msmediaview"},
|
||||
{".m15", "audio/x-mod"},
|
||||
{".m3u", "audio/x-mpegurl"},
|
||||
{".m3url", "audio/x-mpegurl"},
|
||||
{".ma1", "audio/ma1"},
|
||||
{".ma2", "audio/ma2"},
|
||||
{".ma3", "audio/ma3"},
|
||||
{".ma5", "audio/ma5"},
|
||||
{".man", "application/x-troff-man"},
|
||||
{".map", "magnus-internal/imagemap"},
|
||||
{".mbd", "application/mbedlet"},
|
||||
{".mct", "application/x-mascot"},
|
||||
{".mdb", "application/x-msaccess"},
|
||||
{".mdz", "audio/x-mod"},
|
||||
{".me", "application/x-troff-me"},
|
||||
{".mel", "text/x-vmel"},
|
||||
{".mi", "application/x-mif"},
|
||||
{".mid", "audio/midi"},
|
||||
{".midi", "audio/midi"},
|
||||
{".m4a", "audio/mp4a-latm"},
|
||||
{".m4b", "audio/mp4a-latm"},
|
||||
{".m4p", "audio/mp4a-latm"},
|
||||
{".m4u", "video/vnd.mpegurl"},
|
||||
{".m4v", "video/x-m4v"},
|
||||
{".mov", "video/quicktime"},
|
||||
{".mp2", "audio/x-mpeg"},
|
||||
{".mp3", "audio/x-mpeg"},
|
||||
{".mp4", "video/mp4"},
|
||||
{".mpc", "application/vnd.mpohun.certificate"},
|
||||
{".mpe", "video/mpeg"},
|
||||
{".mpeg", "video/mpeg"},
|
||||
{".mpg", "video/mpeg"},
|
||||
{".mpg4", "video/mp4"},
|
||||
{".mpga", "audio/mpeg"},
|
||||
{".msg", "application/vnd.ms-outlook"},
|
||||
{".mif", "application/x-mif"},
|
||||
{".mil", "image/x-cals"},
|
||||
{".mio", "audio/x-mio"},
|
||||
{".mmf", "application/x-skt-lbs"},
|
||||
{".mng", "video/x-mng"},
|
||||
{".mny", "application/x-msmoney"},
|
||||
{".moc", "application/x-mocha"},
|
||||
{".mocha", "application/x-mocha"},
|
||||
{".mod", "audio/x-mod"},
|
||||
{".mof", "application/x-yumekara"},
|
||||
{".mol", "chemical/x-mdl-molfile"},
|
||||
{".mop", "chemical/x-mopac-input"},
|
||||
{".movie", "video/x-sgi-movie"},
|
||||
{".mpn", "application/vnd.mophun.application"},
|
||||
{".mpp", "application/vnd.ms-project"},
|
||||
{".mps", "application/x-mapserver"},
|
||||
{".mrl", "text/x-mrml"},
|
||||
{".mrm", "application/x-mrm"},
|
||||
{".ms", "application/x-troff-ms"},
|
||||
{".mts", "application/metastream"},
|
||||
{".mtx", "application/metastream"},
|
||||
{".mtz", "application/metastream"},
|
||||
{".mzv", "application/metastream"},
|
||||
{".nar", "application/zip"},
|
||||
{".nbmp", "image/nbmp"},
|
||||
{".nc", "application/x-netcdf"},
|
||||
{".ndb", "x-lml/x-ndb"},
|
||||
{".ndwn", "application/ndwn"},
|
||||
{".nif", "application/x-nif"},
|
||||
{".nmz", "application/x-scream"},
|
||||
{".nokia-op-logo", "image/vnd.nok-oplogo-color"},
|
||||
{".npx", "application/x-netfpx"},
|
||||
{".nsnd", "audio/nsnd"},
|
||||
{".nva", "application/x-neva1"},
|
||||
{".oda", "application/oda"},
|
||||
{".oom", "application/x-atlasMate-plugin"},
|
||||
{".ogg", "audio/ogg"},
|
||||
{".pac", "audio/x-pac"},
|
||||
{".pae", "audio/x-epac"},
|
||||
{".pan", "application/x-pan"},
|
||||
{".pbm", "image/x-portable-bitmap"},
|
||||
{".pcx", "image/x-pcx"},
|
||||
{".pda", "image/x-pda"},
|
||||
{".pdb", "chemical/x-pdb"},
|
||||
{".pdf", "application/pdf"},
|
||||
{".pfr", "application/font-tdpfr"},
|
||||
{".pgm", "image/x-portable-graymap"},
|
||||
{".pict", "image/x-pict"},
|
||||
{".pm", "application/x-perl"},
|
||||
{".pmd", "application/x-pmd"},
|
||||
{".png", "image/png"},
|
||||
{".pnm", "image/x-portable-anymap"},
|
||||
{".pnz", "image/png"},
|
||||
{".pot", "application/vnd.ms-powerpoint"},
|
||||
{".ppm", "image/x-portable-pixmap"},
|
||||
{".pps", "application/vnd.ms-powerpoint"},
|
||||
{".ppt", "application/vnd.ms-powerpoint"},
|
||||
{".pqf", "application/x-cprplayer"},
|
||||
{".pqi", "application/cprplayer"},
|
||||
{".prc", "application/x-prc"},
|
||||
{".proxy", "application/x-ns-proxy-autoconfig"},
|
||||
{".prop", "text/plain"},
|
||||
{".ps", "application/postscript"},
|
||||
{".ptlk", "application/listenup"},
|
||||
{".pub", "application/x-mspublisher"},
|
||||
{".pvx", "video/x-pv-pvx"},
|
||||
{".qcp", "audio/vnd.qcelp"},
|
||||
{".qt", "video/quicktime"},
|
||||
{".qti", "image/x-quicktime"},
|
||||
{".qtif", "image/x-quicktime"},
|
||||
{".r3t", "text/vnd.rn-realtext3d"},
|
||||
{".ra", "audio/x-pn-realaudio"},
|
||||
{".ram", "audio/x-pn-realaudio"},
|
||||
{".ras", "image/x-cmu-raster"},
|
||||
{".rdf", "application/rdf+xml"},
|
||||
{".rf", "image/vnd.rn-realflash"},
|
||||
{".rgb", "image/x-rgb"},
|
||||
{".rlf", "application/x-richlink"},
|
||||
{".rm", "audio/x-pn-realaudio"},
|
||||
{".rmf", "audio/x-rmf"},
|
||||
{".rmm", "audio/x-pn-realaudio"},
|
||||
{".rnx", "application/vnd.rn-realplayer"},
|
||||
{".roff", "application/x-troff"},
|
||||
{".rp", "image/vnd.rn-realpix"},
|
||||
{".rpm", "audio/x-pn-realaudio-plugin"},
|
||||
{".rt", "text/vnd.rn-realtext"},
|
||||
{".rte", "x-lml/x-gps"},
|
||||
{".rtf", "application/rtf"},
|
||||
{".rtg", "application/metastream"},
|
||||
{".rtx", "text/richtext"},
|
||||
{".rv", "video/vnd.rn-realvideo"},
|
||||
{".rwc", "application/x-rogerwilco"},
|
||||
{".rar", "application/x-rar-compressed"},
|
||||
{".rc", "text/plain"},
|
||||
{".rmvb", "audio/x-pn-realaudio"},
|
||||
{".s3m", "audio/x-mod"},
|
||||
{".s3z", "audio/x-mod"},
|
||||
{".sca", "application/x-supercard"},
|
||||
{".scd", "application/x-msschedule"},
|
||||
{".sdf", "application/e-score"},
|
||||
{".sea", "application/x-stuffit"},
|
||||
{".sgm", "text/x-sgml"},
|
||||
{".sgml", "text/x-sgml"},
|
||||
{".shar", "application/x-shar"},
|
||||
{".shtml", "magnus-internal/parsed-html"},
|
||||
{".shw", "application/presentations"},
|
||||
{".si6", "image/si6"},
|
||||
{".si7", "image/vnd.stiwap.sis"},
|
||||
{".si9", "image/vnd.lgtwap.sis"},
|
||||
{".sis", "application/vnd.symbian.install"},
|
||||
{".sit", "application/x-stuffit"},
|
||||
{".skd", "application/x-koan"},
|
||||
{".skm", "application/x-koan"},
|
||||
{".skp", "application/x-koan"},
|
||||
{".skt", "application/x-koan"},
|
||||
{".slc", "application/x-salsa"},
|
||||
{".smd", "audio/x-smd"},
|
||||
{".smi", "application/smil"},
|
||||
{".smil", "application/smil"},
|
||||
{".smp", "application/studiom"},
|
||||
{".smz", "audio/x-smd"},
|
||||
{".sh", "application/x-sh"},
|
||||
{".snd", "audio/basic"},
|
||||
{".spc", "text/x-speech"},
|
||||
{".spl", "application/futuresplash"},
|
||||
{".spr", "application/x-sprite"},
|
||||
{".sprite", "application/x-sprite"},
|
||||
{".sdp", "application/sdp"},
|
||||
{".spt", "application/x-spt"},
|
||||
{".src", "application/x-wais-source"},
|
||||
{".stk", "application/hyperstudio"},
|
||||
{".stm", "audio/x-mod"},
|
||||
{".sv4cpio", "application/x-sv4cpio"},
|
||||
{".sv4crc", "application/x-sv4crc"},
|
||||
{".svf", "image/vnd"},
|
||||
{".svg", "image/svg-xml"},
|
||||
{".svh", "image/svh"},
|
||||
{".svr", "x-world/x-svr"},
|
||||
{".swf", "application/x-shockwave-flash"},
|
||||
{".swfl", "application/x-shockwave-flash"},
|
||||
{".t", "application/x-troff"},
|
||||
{".tad", "application/octet-stream"},
|
||||
{".talk", "text/x-speech"},
|
||||
{".tar", "application/x-tar"},
|
||||
{".taz", "application/x-tar"},
|
||||
{".tbp", "application/x-timbuktu"},
|
||||
{".tbt", "application/x-timbuktu"},
|
||||
{".tcl", "application/x-tcl"},
|
||||
{".tex", "application/x-tex"},
|
||||
{".texi", "application/x-texinfo"},
|
||||
{".texinfo", "application/x-texinfo"},
|
||||
{".tgz", "application/x-tar"},
|
||||
{".thm", "application/vnd.eri.thm"},
|
||||
{".tif", "image/tiff"},
|
||||
{".tiff", "image/tiff"},
|
||||
{".tki", "application/x-tkined"},
|
||||
{".tkined", "application/x-tkined"},
|
||||
{".toc", "application/toc"},
|
||||
{".toy", "image/toy"},
|
||||
{".tr", "application/x-troff"},
|
||||
{".trk", "x-lml/x-gps"},
|
||||
{".trm", "application/x-msterminal"},
|
||||
{".tsi", "audio/tsplayer"},
|
||||
{".tsp", "application/dsptype"},
|
||||
{".tsv", "text/tab-separated-values"},
|
||||
{".ttf", "application/octet-stream"},
|
||||
{".ttz", "application/t-time"},
|
||||
{".txt", "text/plain"},
|
||||
{".ult", "audio/x-mod"},
|
||||
{".ustar", "application/x-ustar"},
|
||||
{".uu", "application/x-uuencode"},
|
||||
{".uue", "application/x-uuencode"},
|
||||
{".vcd", "application/x-cdlink"},
|
||||
{".vcf", "text/x-vcard"},
|
||||
{".vdo", "video/vdo"},
|
||||
{".vib", "audio/vib"},
|
||||
{".viv", "video/vivo"},
|
||||
{".vivo", "video/vivo"},
|
||||
{".vmd", "application/vocaltec-media-desc"},
|
||||
{".vmf", "application/vocaltec-media-file"},
|
||||
{".vmi", "application/x-dreamcast-vms-info"},
|
||||
{".vms", "application/x-dreamcast-vms"},
|
||||
{".vox", "audio/voxware"},
|
||||
{".vqe", "audio/x-twinvq-plugin"},
|
||||
{".vqf", "audio/x-twinvq"},
|
||||
{".vql", "audio/x-twinvq"},
|
||||
{".vre", "x-world/x-vream"},
|
||||
{".vrml", "x-world/x-vrml"},
|
||||
{".vrt", "x-world/x-vrt"},
|
||||
{".vrw", "x-world/x-vream"},
|
||||
{".vts", "workbook/formulaone"},
|
||||
{".wax", "audio/x-ms-wax"},
|
||||
{".wbmp", "image/vnd.wap.wbmp"},
|
||||
{".web", "application/vnd.xara"},
|
||||
{".wav", "audio/x-wav"},
|
||||
{".wma", "audio/x-ms-wma"},
|
||||
{".wmv", "audio/x-ms-wmv"},
|
||||
{".wi", "image/wavelet"},
|
||||
{".wis", "application/x-InstallShield"},
|
||||
{".wm", "video/x-ms-wm"},
|
||||
{".wmd", "application/x-ms-wmd"},
|
||||
{".wmf", "application/x-msmetafile"},
|
||||
{".wml", "text/vnd.wap.wml"},
|
||||
{".wmlc", "application/vnd.wap.wmlc"},
|
||||
{".wmls", "text/vnd.wap.wmlscript"},
|
||||
{".wmlsc", "application/vnd.wap.wmlscriptc"},
|
||||
{".wmlscript", "text/vnd.wap.wmlscript"},
|
||||
{".wmv", "video/x-ms-wmv"},
|
||||
{".wmx", "video/x-ms-wmx"},
|
||||
{".wmz", "application/x-ms-wmz"},
|
||||
{".wpng", "image/x-up-wpng"},
|
||||
{".wps", "application/vnd.ms-works"},
|
||||
{".wpt", "x-lml/x-gps"},
|
||||
{".wri", "application/x-mswrite"},
|
||||
{".wrl", "x-world/x-vrml"},
|
||||
{".wrz", "x-world/x-vrml"},
|
||||
{".ws", "text/vnd.wap.wmlscript"},
|
||||
{".wsc", "application/vnd.wap.wmlscriptc"},
|
||||
{".wv", "video/wavelet"},
|
||||
{".wvx", "video/x-ms-wvx"},
|
||||
{".wxl", "application/x-wxl"},
|
||||
{".x-gzip", "application/x-gzip"},
|
||||
{".xar", "application/vnd.xara"},
|
||||
{".xbm", "image/x-xbitmap"},
|
||||
{".xdm", "application/x-xdma"},
|
||||
{".xdma", "application/x-xdma"},
|
||||
{".xdw", "application/vnd.fujixerox.docuworks"},
|
||||
{".xht", "application/xhtml+xml"},
|
||||
{".xhtm", "application/xhtml+xml"},
|
||||
{".xhtml", "application/xhtml+xml"},
|
||||
{".xla", "application/vnd.ms-excel"},
|
||||
{".xlc", "application/vnd.ms-excel"},
|
||||
{".xll", "application/x-excel"},
|
||||
{".xlm", "application/vnd.ms-excel"},
|
||||
{".xls", "application/vnd.ms-excel"},
|
||||
{".xlt", "application/vnd.ms-excel"},
|
||||
{".xlw", "application/vnd.ms-excel"},
|
||||
{".xm", "audio/x-mod"},
|
||||
{".xml", "text/xml"},
|
||||
{".xmz", "audio/x-mod"},
|
||||
{".xpi", "application/x-xpinstall"},
|
||||
{".xpm", "image/x-xpixmap"},
|
||||
{".xsit", "text/xml"},
|
||||
{".xsl", "text/xml"},
|
||||
{".xul", "text/xul"},
|
||||
{".xwd", "image/x-xwindowdump"},
|
||||
{".xyz", "chemical/x-pdb"},
|
||||
{".yz1", "application/x-yz1"},
|
||||
{".z", "application/x-compress"},
|
||||
{".zac", "application/x-zaurus-zac"},
|
||||
{".zip", "application/zip"},
|
||||
{"", "*/*"}
|
||||
};
|
||||
private Context mContext;
|
||||
|
||||
public FileOpenWays(Context context) {
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
public static Intent showOpenTypeDialog(String param) {
|
||||
Log.e("ViChildError", "showOpenTypeDialog");
|
||||
Intent intent = new Intent();
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
Uri uri = Uri.fromFile(new File(param));
|
||||
intent.setDataAndType(uri, "*/*");
|
||||
return intent;
|
||||
}
|
||||
|
||||
/**
|
||||
* --获取文件类型 --
|
||||
*/
|
||||
public static String getMIMEType(String filePath) {
|
||||
String type = "*/*";
|
||||
String fName = filePath;
|
||||
|
||||
int dotIndex = fName.lastIndexOf(".");
|
||||
if (dotIndex < 0) {
|
||||
return type;
|
||||
}
|
||||
|
||||
String end = fName.substring(dotIndex, fName.length()).toLowerCase();
|
||||
if (TextUtils.isEmpty(end)) {
|
||||
return type;
|
||||
}
|
||||
|
||||
for (int i = 0; i < MIME_MapTable.length; i++) {
|
||||
if (end.equals(MIME_MapTable[i][0])) {
|
||||
type = MIME_MapTable[i][1];
|
||||
}
|
||||
}
|
||||
// 如果没查到mimeType,就从系统里查,
|
||||
if (TextUtils.equals(type, "*/*")) {
|
||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(end.substring(1));
|
||||
if (type == null) {
|
||||
type = "*/*";
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
//打开文件时调用
|
||||
public void openFiles(String filesPath) {
|
||||
Uri uri = Uri.parse("file://" + filesPath);
|
||||
Intent intent = new Intent();
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
String type = getMIMEType(filesPath);
|
||||
intent.setDataAndType(uri, type);
|
||||
if (!TextUtils.equals(type, "*/*")) {
|
||||
try {
|
||||
mContext.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
mContext.startActivity(showOpenTypeDialog(filesPath));
|
||||
}
|
||||
} else {
|
||||
mContext.startActivity(showOpenTypeDialog(filesPath));
|
||||
}
|
||||
}
|
||||
|
||||
//显示打开方式
|
||||
public void show(String filesPath) {
|
||||
Intent intent = new Intent();
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
mContext.startActivity(showOpenTypeDialog(filesPath));
|
||||
}
|
||||
|
||||
}
|
||||
755
app/src/main/java/com/ckkj/water/utils/dataUtil/FileUtil.java
Normal file
755
app/src/main/java/com/ckkj/water/utils/dataUtil/FileUtil.java
Normal file
@@ -0,0 +1,755 @@
|
||||
package com.ckkj.water.utils.dataUtil;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.provider.DocumentsContract;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.ckkj.water.utils.show.FL;
|
||||
import com.ckkj.water.utils.show.L;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
/**
|
||||
* 文件操作工具 可以创建和删除文件等
|
||||
*/
|
||||
public class FileUtil {
|
||||
private static final String KB = "KB";
|
||||
private static final String MB = "MB";
|
||||
private static final String GB = "GB";
|
||||
private static final String TAG = "FileUtil";
|
||||
private static final String APPLICATION_FOLDER = "GameBar2";
|
||||
private static final String ROM_FOLDER = "roms";
|
||||
private static final String DOWNLOAD_FOLDER = "downloads";
|
||||
|
||||
private FileUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件夹大小
|
||||
*/
|
||||
public static long getDirSize(String filePath) {
|
||||
long size = 0;
|
||||
File f = new File(filePath);
|
||||
if (f.isDirectory()) {
|
||||
File[] files = f.listFiles();
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
size += getDirSize(file.getPath());
|
||||
continue;
|
||||
}
|
||||
size += file.length();
|
||||
}
|
||||
} else {
|
||||
size += f.length();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储bitmap
|
||||
*/
|
||||
public static void saveBitmap( String filePath, Bitmap bitmap) {
|
||||
File file = createFile(filePath);
|
||||
try {
|
||||
FileOutputStream os = new FileOutputStream(file);
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
|
||||
os.flush();
|
||||
os.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文件大小
|
||||
*
|
||||
* @param size file.length() 获取文件大小
|
||||
*/
|
||||
public static String formatFileSize(double size) {
|
||||
double kiloByte = size / 1024;
|
||||
if (kiloByte < 1) {
|
||||
return size + "B";
|
||||
}
|
||||
|
||||
double megaByte = kiloByte / 1024;
|
||||
if (megaByte < 1) {
|
||||
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
|
||||
return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
|
||||
}
|
||||
|
||||
double gigaByte = megaByte / 1024;
|
||||
if (gigaByte < 1) {
|
||||
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
|
||||
return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
|
||||
}
|
||||
|
||||
double teraBytes = gigaByte / 1024;
|
||||
if (teraBytes < 1) {
|
||||
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
|
||||
return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
|
||||
}
|
||||
BigDecimal result4 = new BigDecimal(teraBytes);
|
||||
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件后缀名
|
||||
*/
|
||||
public static String getFileExtensionName(String fileName) {
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
return "";
|
||||
}
|
||||
int endP = fileName.lastIndexOf(".");
|
||||
return endP > -1 ? fileName.substring(endP + 1, fileName.length()) : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验文件MD5码
|
||||
*/
|
||||
public static boolean checkMD5(String md5, File updateFile) {
|
||||
if (TextUtils.isEmpty(md5) || updateFile == null) {
|
||||
L.e(TAG, "MD5 string empty or updateFile null");
|
||||
return false;
|
||||
}
|
||||
|
||||
String calculatedDigest = getFileMD5(updateFile);
|
||||
if (calculatedDigest == null) {
|
||||
L.e(TAG, "calculatedDigest null");
|
||||
return false;
|
||||
}
|
||||
return calculatedDigest.equalsIgnoreCase(md5);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件MD5码
|
||||
*/
|
||||
public static String getFileMD5(File updateFile) {
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
L.e(TAG, "Exception while getting digest", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream is;
|
||||
try {
|
||||
is = new FileInputStream(updateFile);
|
||||
} catch (FileNotFoundException e) {
|
||||
L.e(TAG, "Exception while getting FileInputStream", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int read;
|
||||
try {
|
||||
while ((read = is.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
byte[] md5sum = digest.digest();
|
||||
BigInteger bigInt = new BigInteger(1, md5sum);
|
||||
String output = bigInt.toString(16);
|
||||
// Fill to 32 chars
|
||||
output = String.format("%32s", output).replace(' ', '0');
|
||||
return output;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Unable to process file for MD5", e);
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
L.e(TAG, "Exception on closing MD5 input stream", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压缩功能.
|
||||
* 将ZIP_FILENAME文件解压到ZIP_DIR目录下.
|
||||
*
|
||||
* @param zipFile 压缩文件
|
||||
* @param folderPath 解压目录
|
||||
*/
|
||||
public static int unZipFile(File zipFile, String folderPath) {
|
||||
ZipFile zfile = null;
|
||||
try {
|
||||
zfile = new ZipFile(zipFile);
|
||||
Enumeration zList = zfile.entries();
|
||||
ZipEntry ze = null;
|
||||
byte[] buf = new byte[1024];
|
||||
while (zList.hasMoreElements()) {
|
||||
ze = (ZipEntry) zList.nextElement();
|
||||
if (ze.isDirectory()) {
|
||||
// L.d(TAG, "ze.getName() = " + ze.getName());
|
||||
String dirstr = folderPath + ze.getName();
|
||||
//dirstr.trim();
|
||||
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
|
||||
// L.d(TAG, "str = " + dirstr);
|
||||
File f = new File(dirstr);
|
||||
f.mkdir();
|
||||
continue;
|
||||
}
|
||||
// L.d(TAG, "ze.getName() = " + ze.getName());
|
||||
OutputStream os = new BufferedOutputStream(
|
||||
new FileOutputStream(getRealFileName(folderPath, ze.getName())));
|
||||
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
|
||||
int readLen = 0;
|
||||
while ((readLen = is.read(buf)) != -1) {
|
||||
os.write(buf, 0, readLen);
|
||||
}
|
||||
is.close();
|
||||
os.close();
|
||||
}
|
||||
zfile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定根目录,返回一个相对路径所对应的实际文件名.
|
||||
*
|
||||
* @param baseDir 指定根目录
|
||||
* @param absFileName 相对路径名,来自于ZipEntry中的name
|
||||
* @return java.io.File 实际的文件
|
||||
*/
|
||||
private static File getRealFileName(String baseDir, String absFileName) {
|
||||
String[] dirs = absFileName.split("/");
|
||||
File ret = new File(baseDir);
|
||||
String substr = null;
|
||||
if (dirs.length > 1) {
|
||||
for (int i = 0; i < dirs.length - 1; i++) {
|
||||
substr = dirs[i];
|
||||
try {
|
||||
//substr.trim();
|
||||
substr = new String(substr.getBytes("8859_1"), "GB2312");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ret = new File(ret, substr);
|
||||
}
|
||||
// L.d("upZipFile", "1ret = " + ret);
|
||||
if (!ret.exists()) ret.mkdirs();
|
||||
substr = dirs[dirs.length - 1];
|
||||
try {
|
||||
//substr.trim();
|
||||
substr = new String(substr.getBytes("8859_1"), "GB2312");
|
||||
// L.d("upZipFile", "substr = " + substr);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ret = new File(ret, substr);
|
||||
// L.d("upZipFile", "2ret = " + ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流创建文件
|
||||
*/
|
||||
public static void createFileFormInputStream(InputStream is, String path) {
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(path);
|
||||
byte[] buf = new byte[1376];
|
||||
while (is.read(buf) > 0) {
|
||||
fos.write(buf, 0, buf.length);
|
||||
}
|
||||
is.close();
|
||||
fos.flush();
|
||||
fos.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件读取对象
|
||||
*/
|
||||
public static Object readObj(String path) {
|
||||
FileInputStream fis = null;
|
||||
ObjectInputStream ois = null;
|
||||
try {
|
||||
fis = new FileInputStream(path);
|
||||
ois = new ObjectInputStream(fis);
|
||||
return ois.readObject();
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(TAG, FL.getExceptionString(e));
|
||||
} catch (IOException e) {
|
||||
FL.e(TAG, FL.getExceptionString(e));
|
||||
} catch (ClassNotFoundException e) {
|
||||
FL.e(TAG, FL.getExceptionString(e));
|
||||
} finally {
|
||||
try {
|
||||
if (fis != null) {
|
||||
fis.close();
|
||||
}
|
||||
if (ois != null) {
|
||||
ois.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
FL.e(TAG, FL.getExceptionString(e));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储对象到文件,只有实现了Serailiable接口的对象才能被存储
|
||||
*/
|
||||
public static void writeObj(String path, Object object) {
|
||||
FileOutputStream fos = null;
|
||||
ObjectOutputStream oos = null;
|
||||
File file = new File(path);
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
try {
|
||||
fos = new FileOutputStream(path);
|
||||
oos = new ObjectOutputStream(fos);
|
||||
oos.writeObject(object);
|
||||
} catch (FileNotFoundException e) {
|
||||
FL.e(TAG, FL.getExceptionString(e));
|
||||
} catch (IOException e) {
|
||||
FL.e(TAG, FL.getExceptionString(e));
|
||||
} finally {
|
||||
try {
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
if (oos != null) {
|
||||
oos.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
FL.e(TAG, FL.getExceptionString(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建文件 当文件不存在的时候就创建一个文件,否则直接返回文件
|
||||
*/
|
||||
public static File createFile(String path) {
|
||||
File file = new File(path);
|
||||
return createFile(file);
|
||||
}
|
||||
|
||||
public static File createFile(File file) {
|
||||
if (!file.getParentFile().exists()) {
|
||||
L.d(TAG, "目标文件所在路径不存在,准备创建……");
|
||||
if (!createDir(file.getParent())) {
|
||||
L.d(TAG, "创建目录文件所在的目录失败!文件路径【" + file.getPath() + "】");
|
||||
}
|
||||
}
|
||||
// 创建目标文件
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
if (file.createNewFile()) {
|
||||
L.d(TAG, "创建文件成功:" + file.getAbsolutePath());
|
||||
}
|
||||
return file;
|
||||
} else {
|
||||
return file;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建目录 当目录不存在的时候创建文件,否则返回false
|
||||
*/
|
||||
public static boolean createDir(String path) {
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
if (!file.mkdirs()) {
|
||||
L.d(TAG, "创建失败,请检查路径和是否配置文件权限!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拷贝文件
|
||||
*/
|
||||
public static boolean copy(String fromPath, String toPath) {
|
||||
File file = new File(fromPath);
|
||||
if (!file.exists()) {
|
||||
return false;
|
||||
}
|
||||
createFile(toPath);
|
||||
return copyFile(fromPath, toPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拷贝文件
|
||||
*/
|
||||
private static boolean copyFile(String fromFile, String toFile) {
|
||||
InputStream fosfrom = null;
|
||||
OutputStream fosto = null;
|
||||
try {
|
||||
fosfrom = new FileInputStream(fromFile);
|
||||
fosto = new FileOutputStream(toFile);
|
||||
byte bt[] = new byte[1024];
|
||||
int c;
|
||||
while ((c = fosfrom.read(bt)) > 0) {
|
||||
fosto.write(bt, 0, c);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (fosfrom != null) {
|
||||
fosfrom.close();
|
||||
}
|
||||
if (fosto != null) {
|
||||
fosto.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件 如果文件存在删除文件,否则返回false
|
||||
*/
|
||||
public static boolean deleteFile(String path) {
|
||||
if (TextUtils.isEmpty(path)) return false;
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除目录下的所有文件及子目录下所有文件
|
||||
*
|
||||
* @param dir 将要删除的文件目录
|
||||
* @return 删除成功返回true,否则返回false,如果文件是空,那么永远返回true
|
||||
*/
|
||||
public static boolean deleteDir(File dir) {
|
||||
if (dir == null) {
|
||||
return true;
|
||||
}
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
// 递归删除目录中的子目录下
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
boolean success = deleteDir(new File(dir, children[i]));
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 目录此时为空,可以删除
|
||||
return dir.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归返回文件或者目录的大小(单位:KB)
|
||||
* 不建议使用这个方法,有点坑
|
||||
* 可以使用下面的方法:http://blog.csdn.net/loongggdroid/article/details/12304695
|
||||
*/
|
||||
private static float getSize(String path, Float size) {
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
if (file.isDirectory()) {
|
||||
String[] children = file.list();
|
||||
for (int fileIndex = 0; fileIndex < children.length; ++fileIndex) {
|
||||
float tmpSize =
|
||||
getSize(file.getPath() + File.separator + children[fileIndex], size) / 1000;
|
||||
size += tmpSize;
|
||||
}
|
||||
} else if (file.isFile()) {
|
||||
size += file.length();
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取apk文件的icon
|
||||
*
|
||||
* @param path apk文件路径
|
||||
*/
|
||||
public static Drawable getApkIcon(Context context, String path) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
|
||||
if (info != null) {
|
||||
ApplicationInfo appInfo = info.applicationInfo;
|
||||
//android有bug,需要下面这两句话来修复才能获取apk图片
|
||||
appInfo.sourceDir = path;
|
||||
appInfo.publicSourceDir = path;
|
||||
// String packageName = appInfo.packageName; //得到安装包名称
|
||||
// String version=info.versionName; //得到版本信息
|
||||
return pm.getApplicationIcon(appInfo);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断有无Sd卡
|
||||
*/
|
||||
public static boolean judgeIsExistSdCard() {
|
||||
if (Environment.getExternalStorageState()
|
||||
.equals(Environment.MEDIA_MOUNTED)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更改文件权限
|
||||
*/
|
||||
public synchronized static void chmodFile(String path) {
|
||||
String[] cmd = { "chmod", "777", path };
|
||||
Process process = null;
|
||||
InputStream errorStream = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(cmd);
|
||||
byte[] buffer = new byte[64];
|
||||
int len = -1;
|
||||
inputStream = process.getInputStream();
|
||||
while ((len = inputStream.read(buffer)) > 0) {
|
||||
|
||||
}
|
||||
errorStream = process.getErrorStream();
|
||||
len = -1;
|
||||
while ((len = errorStream.read(buffer)) > 0) {
|
||||
|
||||
}
|
||||
buffer = null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (errorStream != null) {
|
||||
errorStream.close();
|
||||
}
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
if (process != null) {
|
||||
process.destroy();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用文件夹 /
|
||||
*
|
||||
* @return The application folder.
|
||||
*/
|
||||
public synchronized static File getApplicationFolder(Context mContext) {
|
||||
File root = null;
|
||||
if (judgeIsExistSdCard()) {
|
||||
root = Environment.getExternalStorageDirectory();
|
||||
} else {
|
||||
root = mContext.getFilesDir();
|
||||
}
|
||||
chmodFile(root.getAbsolutePath());
|
||||
if (root != null) {
|
||||
File folder = new File(root, APPLICATION_FOLDER);
|
||||
if (!folder.exists()) {
|
||||
folder.mkdirs();
|
||||
chmodFile(folder.getAbsolutePath());
|
||||
}
|
||||
return folder;
|
||||
} else {
|
||||
root.toString();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 存放应用的文件夹
|
||||
*/
|
||||
public static File getRomloadFolder(Context context) {
|
||||
File root = getApplicationFolder(context);
|
||||
if (root != null) {
|
||||
File folder = new File(root, ROM_FOLDER);
|
||||
if (!folder.exists()) {
|
||||
folder.mkdir();
|
||||
chmodFile(folder.getAbsolutePath());
|
||||
}
|
||||
return folder;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载文件
|
||||
*/
|
||||
public synchronized static File getDownloadFolder(Context mContext) {
|
||||
File root = getApplicationFolder(mContext);
|
||||
if (root != null) {
|
||||
File folder = new File(root, DOWNLOAD_FOLDER);
|
||||
if (!folder.exists()) {
|
||||
folder.mkdirs();
|
||||
chmodFile(folder.getAbsolutePath());
|
||||
}
|
||||
return folder;
|
||||
} else {
|
||||
root.toString();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//判断是否存在sd卡
|
||||
private static boolean getSDCardisExist() {
|
||||
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//获取sd卡根路径
|
||||
private static String getSDPath() {
|
||||
File sdDir = null;
|
||||
boolean sdCardExist = (Environment.getExternalStorageState())
|
||||
.equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
|
||||
if (sdCardExist) {
|
||||
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
|
||||
}
|
||||
return sdDir.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
public static List<MultipartBody.Part> files2Parts(String key,
|
||||
List<File> files) {
|
||||
List<MultipartBody.Part> parts = new ArrayList<>();
|
||||
if (files.size()>0){
|
||||
for (File file : files) {
|
||||
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
|
||||
MultipartBody.Part part = MultipartBody.Part.
|
||||
createFormData(key, file.getName(), requestBody);
|
||||
parts.add(part);
|
||||
}
|
||||
}else {
|
||||
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), "");
|
||||
MultipartBody.Part part = MultipartBody.Part.createFormData(key, "", requestBody);
|
||||
parts.add(part);
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
|
||||
public static String getAbsolutePathFromUri(Context context,Uri uri) {
|
||||
String path = null;
|
||||
|
||||
// 使用 ContentResolver 获取路径
|
||||
if ("content".equalsIgnoreCase(uri.getScheme())) {
|
||||
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
|
||||
if (cursor != null) {
|
||||
int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
cursor.moveToFirst();
|
||||
String displayName = cursor.getString(columnIndex);
|
||||
cursor.close();
|
||||
// 这里可以使用 displayName 进行后续处理
|
||||
// 注意:这不是绝对路径,只是文件名
|
||||
path = displayName;
|
||||
}
|
||||
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
|
||||
path = uri.getPath();
|
||||
} else if (DocumentsContract.isDocumentUri(context, uri)) {
|
||||
String documentId = DocumentsContract.getDocumentId(uri);
|
||||
String[] split = documentId.split(":");
|
||||
String type = split[0];
|
||||
|
||||
if ("primary".equalsIgnoreCase(type)) {
|
||||
path = Environment.getExternalStorageDirectory() + "/" + split[1];
|
||||
} else {
|
||||
// 处理非主存储的情况
|
||||
// 这里可能需要根据具体存储类型进行处理
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 文件后缀名
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static String getFileSuffixWithQuestion(String url) {
|
||||
if (url == null || url.isEmpty()) return "";
|
||||
|
||||
// 去掉问号之后的部分
|
||||
int questionMarkIndex = url.indexOf("?");
|
||||
if (questionMarkIndex != -1) {
|
||||
url = url.substring(0, questionMarkIndex);
|
||||
}
|
||||
|
||||
// 获取最后一个点后面的内容作为后缀
|
||||
int lastDotIndex = url.lastIndexOf(".");
|
||||
if (lastDotIndex != -1 && lastDotIndex < url.length() - 1) {
|
||||
return url.substring(lastDotIndex); // 包含“.”的后缀
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.ckkj.water.utils.dataUtil;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PhonePatternUtil {
|
||||
private static final String PHONE_REGEX = "^(1(3[0-9]|4[5-9]|5[0-3,5-9]|6[5-6]|7[0-8]|8[0-9]|9[1,3,5,8,9])\\d{8})$";
|
||||
private static final Pattern PHONE_PATTERN = Pattern.compile(PHONE_REGEX);
|
||||
|
||||
public static boolean phoneReg(String phoneNumber) {
|
||||
if (isValidPhoneNumber(phoneNumber)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isValidPhoneNumber(String phoneNumber) {
|
||||
return PHONE_PATTERN.matcher(phoneNumber).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package com.ckkj.water.utils.dataUtil;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.ckkj.water.ConstantUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 配置文件工具类
|
||||
*
|
||||
* @author “AriaLyy@outlook.com”
|
||||
*/
|
||||
public class SharePreUtil {
|
||||
|
||||
/**
|
||||
* 删除键值对
|
||||
*/
|
||||
public static void removeKey(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.remove(key);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件读取字符串
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 字符串键值
|
||||
* @return 键值对应的字符串, 默认返回""
|
||||
*/
|
||||
public static String getString(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
if(ConstantUtil.USER_LOCATION_PROVICE_NAME.equals(key)){
|
||||
return pre.getString(key, "");
|
||||
}else if(ConstantUtil.USER_LOCATION_CITY_NAME.equals(key)){
|
||||
return pre.getString(key, "");
|
||||
}
|
||||
return pre.getString(key, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件读取int数据
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key int的键值
|
||||
* @return 键值对应的int, 默认返回-1
|
||||
*/
|
||||
public static int getInt(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
return pre.getInt(key, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件读取Boolean值
|
||||
*
|
||||
* @return 如果没有,默认返回false
|
||||
*/
|
||||
public static Boolean getBoolean(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
return pre.getBoolean(key, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件读取Boolean值
|
||||
*
|
||||
* @return 如果没有,默认返回false
|
||||
*/
|
||||
public static Boolean getBooleanDefTrue(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
return pre.getBoolean(key, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件获取float数据
|
||||
*
|
||||
* @return 默认返回0.0f
|
||||
*/
|
||||
public static float getFloat(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
return pre.getFloat(key, 0.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件获取对象
|
||||
*/
|
||||
public static <T> T getObject(String preName, Context context, String key, Class<T> clazz) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
String str = pre.getString(key, "");
|
||||
return TextUtils.isEmpty(str) ? null : new Gson().fromJson(str, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储字符串到配置文件
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 存储的键值
|
||||
* @param value 需要存储的字符串
|
||||
* @return 成功标志
|
||||
*/
|
||||
public static Boolean putString(String preName, Context context, String key, String value) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.putString(key, value);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存Float数据到配置文件
|
||||
*/
|
||||
public static Boolean putFloat(String preName, Context context, String key, float value) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.putFloat(key, value);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储数字到配置文件
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 存储的键值
|
||||
* @param value 需要存储的数字
|
||||
* @return 成功标志
|
||||
*/
|
||||
public static Boolean putInt(String preName, Context context, String key, int value) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.putInt(key, value);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储Boolean值到配置文件
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 键值
|
||||
* @param value 需要存储的boolean值
|
||||
*/
|
||||
public static Boolean putBoolean(String preName, Context context, String key, Boolean value) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.putBoolean(key, value);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储Long值到配置文件
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 键值
|
||||
* @param value 需要存储的boolean值
|
||||
*/
|
||||
public static Boolean putLong(String preName, Context context, String key, long value) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.putLong(key, value);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从配置文件读取int数据
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key int的键值
|
||||
* @return 键值对应的int, 默认返回-1
|
||||
*/
|
||||
public static long getLongDefaulTime(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
return pre.getLong(key, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 存放对象
|
||||
*/
|
||||
public static Boolean putObject(String preName, Context context, String key, Class<?> clazz,
|
||||
Object obj) {
|
||||
String str = new Gson().toJson(obj, clazz);
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
Editor editor = pre.edit();
|
||||
editor.putString(key, str);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
//偏好设置存储list
|
||||
public static <T> void saveVideoSP(Context context, String tableName, String key, List<T> datalist) {
|
||||
SharedPreferences preferences = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
|
||||
Editor editor = preferences.edit();
|
||||
Gson gson = new Gson();
|
||||
String strJson = gson.toJson(datalist);
|
||||
editor.clear();
|
||||
editor.putString(key, strJson);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
|
||||
//偏好设置取出list
|
||||
public static <T> List<T> getModelList(Context context, String tableName, String key,Class<T> cls) {
|
||||
if (context == null || tableName == null || key == null) {
|
||||
return null;
|
||||
}
|
||||
SharedPreferences sharedPreferences = context.getSharedPreferences(tableName, Context.MODE_PRIVATE);
|
||||
List<T> datalist = new ArrayList<>();
|
||||
String strJson = sharedPreferences.getString(key, null);
|
||||
if (strJson == null) {
|
||||
return datalist;
|
||||
}
|
||||
JsonArray jsonArray = new JsonParser().parse(strJson).getAsJsonArray();
|
||||
Gson gson = new Gson();
|
||||
for (JsonElement jsonElement : jsonArray) {
|
||||
datalist.add(gson.fromJson(jsonElement,cls)); //cls
|
||||
}
|
||||
return datalist;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user