1. image and video thumbnail tools
Import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. media. thumbnailUtils;/*** tool class for generating thumbnails * @ author **/public class ThumbnailGenerateUtils {private ThumbnailGenerateUtils (){}; /*** obtain the thumbnail Based on the specified image path and size * This method has two advantages: * 1. using a small memory space, the bitmap obtained for the first time is actually null, just to read the width and height. * The bitmap read for the second time is the image compressed proportionally, the third bitmap read is the desired thumbnail. * 2. The thumbnail is not stretched for the original image. Here we use the new ThumbnailUtils tool of Version 2.2, so that * images generated using this tool will not be stretched. * @ Param imagePath image path * @ param width specifies the width of the output image * @ param height specifies the height of the output image * @ return generated thumbnail */public static Bitmap getImageThumbnail (String imagePath, int width, int height) {Bitmap bitmap = null; BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; // obtain the width and height of the image. Note that bitmap here is null bitmap = BitmapFactory. decodeFile (imagePath, options); options. inJustDeco DeBounds = false; // set it to false // calculate the zoom ratio int h = options. outHeight; int w = options. outWidth; int beWidth = w/width; int beHeight = h/height; int be = 1; if (beWidth <beHeight) {be = beWidth;} else {be = beHeight ;} if (be <= 0) {be = 1;} options. inSampleSize = be; // read the image again and read the scaled bitmap. inJustDecodeBounds is set to false bitmap = BitmapFactory. decodeFile (imagePath, options); // use Thumbn AilUtils to create a thumbnail. here you need to specify which Bitmap object to scale bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT); return bitmap;}/*** get the video thumbnail * first create a thumbnail of the video through ThumbnailUtils, and then use ThumbnailUtils to generate a thumbnail of the specified size. * If the width and height of the desired thumbnail are smaller than that of MICRO_KIND, use MICRO_KIND as the kind value, which saves memory. * @ Param videoPath the video path * @ param width specifies the width of the output video thumbnail * @ param height specifies the height of the output video thumbnail * @ param kind refer to MediaStore. images. the constants MINI_KIND and MICRO_KIND In the Thumbnails class. * MINI_KIND: 512x384, MICRO_KIND: 96x96 * @ return specifies the video thumbnail size */public static Bitmap getVideoThumbnail (String videoPath, int width, int height, int kind) {Bitmap bitmap = null; // obtain the video thumbnail bitmap = ThumbnailUtils. createVideoThumbnail (videoPath, kind); System. out. println ("w" + bitmap. getWidth (); System. out. println ("h" + bitmap. getHeight (); bitmap = ThumbnailUtils. extractThumbnail (bitmap, width, height, ThumbnailUtils. OPTIONS_RECYCLE_INPUT); return bitmap ;}}
2. density computing tools
/*** Density calculation tool ** @ author zbzhangc **/public class DensityUtils {public static int dip2px (Context context, float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);} public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f );}}
3. Create a folder and replace the file name
Import java. io. file; import java. text. simpleDateFormat; import java. util. date; import android. OS. environment;/*** file name Operation tool class * @ author zhang **/public class FileNameOperationUtils {private static SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy. MM. dd "); private FileNameOperationUtils () {};/*** generate folder * @ return folder path */public static String generateFolderName (String projectName) {String folderPath = Environment. getExternalStorageDirectory () + "/Province" + "/" + projectName + "/" + dateFormat. format (new Date (System. currentTimeMillis (); File folder = new File (folderPath); if (! Folder. exists () // create folder. mkdirs (); return folderPath;}/*** get the image file name * @ return */public static String getPictrueFileName () {return System. currentTimeMillis ()/1000 + ". jpg ";}/*** get the video file name * @ return */public static String getVideoFileName () {return System. currentTimeMillis ()/1000 + ". mp4 ";}/*** get the audio file name * @ return */public static String getAudioFileName () {return System. currentTimeMillis ()/1000 + ". 3gp "; }/*** Get the full path name of the image file * @ return */public static String getPictureAbsoluteFileName (String projectName) {return generateFolderName (projectName) + "/" + getPictrueFileName ();}/*** get the full path name of the audio file * @ param projectName * @ return */public static String getAudioAbsoluteFileName (String projectName) {return generateFolderName (projectName) + "/" + getAudioFileName ();}/*** replace folder name * @ param fileName * @ param newFolderNam E * @ return */public static boolean renameFolder (String fileName, String newFolderName) {File file = new File (fileName); if (! File. isDirectory () {String folderPath = file. getPath (). substring (0, file. getPath (). lastIndexOf ("\"); // current folder name String oldFolderName = folderPath. substring (folderPath. lastIndexOf ("\") + 1); // you need to replace the folder name return new File (folderPath ). renameTo (new File (folderPath. replace (oldFolderName, newFolderName);} else {System. out. println (file. getPath (); String oldFolderName = file. getPath (). substring (file. getPath (). lastIndexOf ("\") + 1); System. out. println (oldFolderName); return file. renameTo (new File (file. getPath (). replace (oldFolderName, newFolderName )));}}}
4. Prevent users' continuous clicks
Package com. iss. starwish. util; import android. content. context; import android. widget. toast;/*** prevents the button from clicking * @ author zhang **/public class Utils {private static long lastClickTime; /*** Prevent Users From clicking continuously in MS **/public static boolean isFastDoubleClick () {long time = System. currentTimeMillis (); long timeD = time-lastClickTime; if (0 <timeD & timeD <800) {return true;} lastClickTime = time; return false ;} /*** display Toast * @ param context * @ param content */public static void show (Context context, String content) {Toast. makeText (context, content, Toast. LENGTH_SHORT ). show ();}/*** display Toast * @ param context * @ param content */public static void show (Context context, int strId) {Toast. makeText (context, strId, Toast. LENGTH_SHORT ). show ();}}
5. Convert Chinese characters to PinYin
This and the use of a pinyin4j-2.5.0.jar (also I find on the Internet more reliable conversion method)
Package net. tianyouwang. utils; import net. sourceforge. pinyin4j. pinyinHelper; import net. sourceforge. pinyin4j. format. hanyuPinyinCaseType; import net. sourceforge. pinyin4j. format. hanyuPinyinOutputFormat; import net. sourceforge. pinyin4j. format. hanyuPinyinToneType; import net. sourceforge. pinyin4j. format. hanyuPinyinVCharType; import net. sourceforge. pinyin4j. format. exception. badHanyuPinyinOutputFormatCombinatio N; public class ChineseToPinyinUtil {private ChineseToPinyinUtil () {}/ *** * @ param src * @ return */public static String getPingYin (String src) {char [] t1 = null; t1 = src. toCharArray (); String [] t2 = new String [t1.length]; HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat (); t3.setCaseType (HanyuPinyinCaseType. LOWERCASE); t3.setToneType (HanyuPinyinToneType. WITHOUT_TONE); t3.setVCharType (H AnyuPinyinVCharType. WITH_V); String t4 = ""; int t0 = t1.length; try {for (int I = 0; I <t0; I ++) {// determine whether it is a Chinese character if (java. lang. character. toString (t1 [I]). matches ("[\ u4E00-\ u9FA5] +") {t2 = PinyinHelper. toHanyuPinyinStringArray (t1 [I], t3); t4 + = t2 [0];} elset4 + = java. lang. character. toString (t1 [I]);} // System. out. println (t4); return t4;} catch (BadHanyuPinyinOutputFormatCombination e1) {e1.printStackTrace () ;} Return t4;} // returns the Chinese initial public static String getPinYinHeadChar (String str) {String convert = ""; for (int j = 0; j <str. length (); j ++) {char word = str. charAt (j); String [] pinyinArray = PinyinHelper. toHanyuPinyinStringArray (word); if (pinyinArray! = Null) {convert + = pinyinArray [0]. charAt (0) ;}else {convert + = word ;}} return convert ;}}
6. Determine whether the current network connection type and network are available
Package net. tianyouwang. utils; import android. content. context; import android.net. connectivityManager; import android.net. networkInfo; import android. telephony. telephonyManager; public class NetUtils {/***** determine whether the current network is connected ** @ param con * @ return */public static boolean isNetworkAvailable (Context context) {ConnectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERV ICE); if (cm = null) return false; NetworkInfo netinfo = cm. getActiveNetworkInfo (); if (netinfo = null) {return false;} if (netinfo. isConnected () {return true;} return false ;} /***** obtain the current network type * @ param context * @ return */public static String getNetWorkType (Context context) {ConnectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); if (cm = Null) return ""; NetworkInfo netinfo = cm. getActiveNetworkInfo (); if (netinfo! = Null) {int type = netinfo. getType (); if (type = 0) {// phone int subtype = netinfo. getSubtype (); if (subtype = TelephonyManager. NETWORK_TYPE_CDMA) {// China Telecom 2G return "2g";} else if (subtype = TelephonyManager. NETWORK_TYPE_EVDO_0 | subtype = TelephonyManager. NETWORK_TYPE_EVDO_A | subtype = TelephonyManager. NETWORK_TYPE_EVDO_ B) {// China Telecom 3G return "3g";} else if (subtype = TelephonyManager. NETWORK_TYPE_GPRS) {// China Unicom 2g return "2g";} else if (subtype = TelephonyManager. NETWORK_TYPE_EDGE) {// move 2G return "2g";} else if (subtype = TelephonyManager. NETWORK_TYPE_HSDPA | subtype = TelephonyManager. NETWORK_TYPE_UMTS) {// China Unicom 3g return "3g" ;}} else if (type = 1) {// wifi return "wifi ";}} return "3g ";}}