Android中的工具类总结一

来源:互联网 发布:电子商城微信源码 编辑:程序博客网 时间:2024/06/10 15:37

1.图片处理工具类

[java] view plaincopyprint?
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Canvas;  
  10. import android.graphics.ColorMatrix;  
  11. import android.graphics.ColorMatrixColorFilter;  
  12. import android.graphics.Paint;  
  13. import android.graphics.PorterDuffXfermode;  
  14. import android.graphics.Rect;  
  15. import android.graphics.RectF;  
  16. import android.graphics.Bitmap.Config;  
  17. import android.graphics.PorterDuff.Mode;  
  18. import android.graphics.drawable.BitmapDrawable;  
  19. import android.graphics.drawable.Drawable;  
  20.   
  21. /** 
  22.  * 处理图片的工具类. 
  23.  */  
  24. public class imageTool {  
  25.     public static final int LEFT = 0;  
  26.     public static final int RIGHT = 1;  
  27.     public static final int TOP = 3;  
  28.     public static final int BOTTOM = 4;  
  29.   
  30.     /** */  
  31.     /** 
  32.      * 图片去色,返回灰度图片 
  33.      *  
  34.      * @param bmpOriginal 
  35.      *            传入的图片 
  36.      * @return 去色后的图片 
  37.      */  
  38.     public static Bitmap toGrayscale(Bitmap bmpOriginal) {  
  39.         int width, height;  
  40.         height = bmpOriginal.getHeight();  
  41.         width = bmpOriginal.getWidth();  
  42.         Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,  
  43.                 Bitmap.Config.RGB_565);  
  44.         Canvas c = new Canvas(bmpGrayscale);  
  45.         Paint paint = new Paint();  
  46.         ColorMatrix cm = new ColorMatrix();  
  47.         cm.setSaturation(0);  
  48.         ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);  
  49.         paint.setColorFilter(f);  
  50.         c.drawBitmap(bmpOriginal, 00, paint);  
  51.         return bmpGrayscale;  
  52.     }  
  53.   
  54.     /** */  
  55.     /** 
  56.      * 去色同时加圆角 
  57.      *  
  58.      * @param bmpOriginal 
  59.      *            原图 
  60.      * @param pixels 
  61.      *            圆角弧度 
  62.      * @return 修改后的图片 
  63.      */  
  64.     public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {  
  65.         return toRoundCorner(toGrayscale(bmpOriginal), pixels);  
  66.     }  
  67.   
  68.     /** */  
  69.     /** 
  70.      * 把图片变成圆角 
  71.      *  
  72.      * @param bitmap 
  73.      *            需要修改的图片 
  74.      * @param pixels 
  75.      *            圆角的弧度 
  76.      * @return 圆角图片 
  77.      */  
  78.     public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  
  79.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap  
  80.                 .getHeight(), Config.ARGB_8888);  
  81.         Canvas canvas = new Canvas(output);  
  82.         final int color = 0xff424242;  
  83.         final Paint paint = new Paint();  
  84.         final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
  85.         final RectF rectF = new RectF(rect);  
  86.         final float roundPx = pixels;  
  87.         paint.setAntiAlias(true);  
  88.         canvas.drawARGB(0000);  
  89.         paint.setColor(color);  
  90.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  91.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  92.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  93.         return output;  
  94.     }  
  95.   
  96.     /** */  
  97.     /** 
  98.      * 使圆角功能支持BitampDrawable 
  99.      *  
  100.      * @param bitmapDrawable 
  101.      * @param pixels 
  102.      * @return 
  103.      */  
  104.     public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable,  
  105.             int pixels) {  
  106.         Bitmap bitmap = bitmapDrawable.getBitmap();  
  107.         bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));  
  108.         return bitmapDrawable;  
  109.     }  
  110.   
  111.     /** 
  112.      * 读取路径中的图片,然后将其转化为缩放后的bitmap 
  113.      *  
  114.      * @param path 
  115.      */  
  116.     public static void saveBefore(String path) {  
  117.         BitmapFactory.Options options = new BitmapFactory.Options();  
  118.         options.inJustDecodeBounds = true;  
  119.         // 获取这个图片的宽和高  
  120.         Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空  
  121.         options.inJustDecodeBounds = false;  
  122.         // 计算缩放比  
  123.         int be = (int) (options.outHeight / (float200);  
  124.         if (be <= 0)  
  125.             be = 1;  
  126.         options.inSampleSize = 2// 图片长宽各缩小二分之一  
  127.         // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦  
  128.         bitmap = BitmapFactory.decodeFile(path, options);  
  129.         int w = bitmap.getWidth();  
  130.         int h = bitmap.getHeight();  
  131.         System.out.println(w + " " + h);  
  132.         // savePNG_After(bitmap,path);  
  133.         saveJPGE_After(bitmap, path);  
  134.     }  
  135.   
  136.     /** 
  137.      * 保存图片为PNG 
  138.      *  
  139.      * @param bitmap 
  140.      * @param name 
  141.      */  
  142.     public static void savePNG_After(Bitmap bitmap, String name) {  
  143.         File file = new File(name);  
  144.         try {  
  145.             FileOutputStream out = new FileOutputStream(file);  
  146.             if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {  
  147.                 out.flush();  
  148.                 out.close();  
  149.             }  
  150.         } catch (FileNotFoundException e) {  
  151.             e.printStackTrace();  
  152.         } catch (IOException e) {  
  153.             e.printStackTrace();  
  154.         }  
  155.     }  
  156.   
  157.     /** 
  158.      * 保存图片为JPEG 
  159.      *  
  160.      * @param bitmap 
  161.      * @param path 
  162.      */  
  163.     public static void saveJPGE_After(Bitmap bitmap, String path) {  
  164.         File file = new File(path);  
  165.         try {  
  166.             FileOutputStream out = new FileOutputStream(file);  
  167.             if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {  
  168.                 out.flush();  
  169.                 out.close();  
  170.             }  
  171.         } catch (FileNotFoundException e) {  
  172.             e.printStackTrace();  
  173.         } catch (IOException e) {  
  174.             e.printStackTrace();  
  175.         }  
  176.     }  
  177.   
  178.     /** 
  179.      * 水印 
  180.      *  
  181.      * @param bitmap 
  182.      * @return 
  183.      */  
  184.     public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) {  
  185.         if (src == null) {  
  186.             return null;  
  187.         }  
  188.         int w = src.getWidth();  
  189.         int h = src.getHeight();  
  190.         int ww = watermark.getWidth();  
  191.         int wh = watermark.getHeight();  
  192.         // create the new blank bitmap  
  193.         Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图  
  194.         Canvas cv = new Canvas(newb);  
  195.         // draw src into  
  196.         cv.drawBitmap(src, 00null);// 在 0,0坐标开始画入src  
  197.         // draw watermark into  
  198.         cv.drawBitmap(watermark, w - ww + 5, h - wh + 5null);// 在src的右下角画入水印  
  199.         // save all clip  
  200.         cv.save(Canvas.ALL_SAVE_FLAG);// 保存  
  201.         // store  
  202.         cv.restore();// 存储  
  203.         return newb;  
  204.     }  
  205.   
  206.     /** 
  207.      * 图片合成 
  208.      *  
  209.      * @return 
  210.      */  
  211.     public static Bitmap potoMix(int direction, Bitmap... bitmaps) {  
  212.         if (bitmaps.length <= 0) {  
  213.             return null;  
  214.         }  
  215.         if (bitmaps.length == 1) {  
  216.             return bitmaps[0];  
  217.         }  
  218.         Bitmap newBitmap = bitmaps[0];  
  219.         // newBitmap = createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction);  
  220.         for (int i = 1; i < bitmaps.length; i++) {  
  221.             newBitmap = createBitmapForFotoMix(newBitmap, bitmaps[i], direction);  
  222.         }  
  223.         return newBitmap;  
  224.     }  
  225.   
  226.     private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second,  
  227.             int direction) {  
  228.         if (first == null) {  
  229.             return null;  
  230.         }  
  231.         if (second == null) {  
  232.             return first;  
  233.         }  
  234.         int fw = first.getWidth();  
  235.         int fh = first.getHeight();  
  236.         int sw = second.getWidth();  
  237.         int sh = second.getHeight();  
  238.         Bitmap newBitmap = null;  
  239.         if (direction == LEFT) {  
  240.             newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh,  
  241.                     Config.ARGB_8888);  
  242.             Canvas canvas = new Canvas(newBitmap);  
  243.             canvas.drawBitmap(first, sw, 0null);  
  244.             canvas.drawBitmap(second, 00null);  
  245.         } else if (direction == RIGHT) {  
  246.             newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh,  
  247.                     Config.ARGB_8888);  
  248.             Canvas canvas = new Canvas(newBitmap);  
  249.             canvas.drawBitmap(first, 00null);  
  250.             canvas.drawBitmap(second, fw, 0null);  
  251.         } else if (direction == TOP) {  
  252.             newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh,  
  253.                     Config.ARGB_8888);  
  254.             Canvas canvas = new Canvas(newBitmap);  
  255.             canvas.drawBitmap(first, 0, sh, null);  
  256.             canvas.drawBitmap(second, 00null);  
  257.         } else if (direction == BOTTOM) {  
  258.             newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh,  
  259.                     Config.ARGB_8888);  
  260.             Canvas canvas = new Canvas(newBitmap);  
  261.             canvas.drawBitmap(first, 00null);  
  262.             canvas.drawBitmap(second, 0, fh, null);  
  263.         }  
  264.         return newBitmap;  
  265.     }  
  266.   
  267.     /** 
  268.      * 将Bitmap转换成指定大小 
  269.      *  
  270.      * @param bitmap 
  271.      * @param width 
  272.      * @param height 
  273.      * @return 
  274.      */  
  275.     public static Bitmap createBitmapBySize(Bitmap bitmap, int width, int height) {  
  276.         return Bitmap.createScaledBitmap(bitmap, width, height, true);  
  277.     }  
  278.   
  279.     /** 
  280.      * Drawable 转 Bitmap 
  281.      *  
  282.      * @param drawable 
  283.      * @return 
  284.      */  
  285.     public static Bitmap drawableToBitmapByBD(Drawable drawable) {  
  286.         BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;  
  287.         return bitmapDrawable.getBitmap();  
  288.     }  
  289.   
  290.     /** 
  291.      * Bitmap 转 Drawable 
  292.      *  
  293.      * @param bitmap 
  294.      * @return 
  295.      */  
  296.     public static Drawable bitmapToDrawableByBD(Bitmap bitmap) {  
  297.         Drawable drawable = new BitmapDrawable(bitmap);  
  298.         return drawable;  
  299.     }  
  300.   
  301.     /** 
  302.      * byte[] 转 bitmap 
  303.      *  
  304.      * @param b 
  305.      * @return 
  306.      */  
  307.     public static Bitmap bytesToBimap(byte[] b) {  
  308.         if (b.length != 0) {  
  309.             return BitmapFactory.decodeByteArray(b, 0, b.length);  
  310.         } else {  
  311.             return null;  
  312.         }  
  313.     }  
  314.   
  315.     /** 
  316.      * bitmap 转 byte[] 
  317.      *  
  318.      * @param bm 
  319.      * @return 
  320.      */  
  321.     public static byte[] bitmapToBytes(Bitmap bm) {  
  322.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  323.         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  324.         return baos.toByteArray();  
  325.     }  
  326. }  


2.获取网络状态工具类

  1. package com.todoo.android.app.utils;  
  2.   
  3. import java.net.InetAddress;  
  4. import java.net.NetworkInterface;  
  5. import java.net.SocketException;  
  6. import java.util.Enumeration;  
  7.   
  8. import android.content.Context;  
  9. import android.net.ConnectivityManager;  
  10. import android.net.NetworkInfo;  
  11.   
  12.   
  13. public class NetworkUtils  
  14. {  
  15.     public static final String NET_TYPE_WIFI = "WIFI";  
  16.     public static final String NET_TYPE_MOBILE = "MOBILE";  
  17.     public static final String NET_TYPE_NO_NETWORK = "no_network";  
  18.       
  19.     private Context mContext = null;  
  20.       
  21.     public NetworkUtils(Context pContext) {  
  22.         this.mContext = pContext;  
  23.     }  
  24.       
  25.     public static final String IP_DEFAULT = "0.0.0.0";  
  26.   
  27.     public static boolean isConnectInternet(final Context pContext)  
  28.     {  
  29.         final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);  
  30.         final NetworkInfo networkInfo = conManager.getActiveNetworkInfo();  
  31.   
  32.         if (networkInfo != null)  
  33.         {  
  34.             return networkInfo.isAvailable();  
  35.         }  
  36.   
  37.         return false;  
  38.     }  
  39.       
  40.     public static boolean isConnectWifi(final Context pContext) {  
  41.         ConnectivityManager mConnectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);  
  42.         NetworkInfo info = mConnectivity.getActiveNetworkInfo();  
  43.         //判断网络连接类型,只有在3G或wifi里进行一些数据更新。    
  44.         int netType = -1;  
  45.         if(info != null){  
  46.             netType = info.getType();  
  47.         }  
  48.         if (netType == ConnectivityManager.TYPE_WIFI) {  
  49.             return info.isConnected();  
  50.         } else {  
  51.             return false;  
  52.         }  
  53.     }  
  54.   
  55.     public static String getNetTypeName(final int pNetType)  
  56.     {  
  57.         switch (pNetType)  
  58.         {  
  59.             case 0:  
  60.                 return "unknown";  
  61.             case 1:  
  62.                 return "GPRS";  
  63.             case 2:  
  64.                 return "EDGE";  
  65.             case 3:  
  66.                 return "UMTS";  
  67.             case 4:  
  68.                 return "CDMA: Either IS95A or IS95B";  
  69.             case 5:  
  70.                 return "EVDO revision 0";  
  71.             case 6:  
  72.                 return "EVDO revision A";  
  73.             case 7:  
  74.                 return "1xRTT";  
  75.             case 8:  
  76.                 return "HSDPA";  
  77.             case 9:  
  78.                 return "HSUPA";  
  79.             case 10:  
  80.                 return "HSPA";  
  81.             case 11:  
  82.                 return "iDen";  
  83.             case 12:  
  84.                 return "EVDO revision B";  
  85.             case 13:  
  86.                 return "LTE";  
  87.             case 14:  
  88.                 return "eHRPD";  
  89.             case 15:  
  90.                 return "HSPA+";  
  91.             default:  
  92.                 return "unknown";  
  93.         }  
  94.     }  
  95.   
  96.     public static String getIPAddress()  
  97.     {  
  98.         try  
  99.         {  
  100.             final Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();  
  101.   
  102.             while (networkInterfaceEnumeration.hasMoreElements())  
  103.             {  
  104.                 final NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();  
  105.   
  106.                 final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();  
  107.   
  108.                 while (inetAddressEnumeration.hasMoreElements())  
  109.                 {  
  110.                     final InetAddress inetAddress = inetAddressEnumeration.nextElement();  
  111.   
  112.                     if (!inetAddress.isLoopbackAddress())  
  113.                     {  
  114.                         return inetAddress.getHostAddress();  
  115.                     }  
  116.                 }  
  117.             }  
  118.   
  119.             return NetworkUtils.IP_DEFAULT;  
  120.         }  
  121.         catch (final SocketException e)  
  122.         {  
  123.             return NetworkUtils.IP_DEFAULT;  
  124.         }  
  125.     }  
  126.       
  127.     public String getConnTypeName() {  
  128.         ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);  
  129.         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();  
  130.         if(networkInfo == null) {  
  131.             return NET_TYPE_NO_NETWORK;  
  132.         } else {  
  133.             return networkInfo.getTypeName();  
  134.         }  
  135.     }  
  136. }  
[html] view plaincopyprint?
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  2.    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

3.Android中资源存取工具类


  1. /** 
  2.  * 资源管理类 
  3.  * @author: linxcool.hu 
  4.  */  
  5. public class ResourceManager {  
  6.     private static final String TAG="ResourceManager";  
  7.     private static ResourceManager resourceManager=new ResourceManager();  
  8.     private List<Bitmap> bitmapCaches;  
  9.       
  10.     private ResourceManager(){  
  11.         bitmapCaches=new ArrayList<Bitmap>();  
  12.     }  
  13.     public static ResourceManager getInstance(){  
  14.         return resourceManager;  
  15.     }  
  16.     /** 
  17.      * 检查SD卡是否存在 
  18.      * @return 存在返回true,否则返回false 
  19.      */  
  20.     public boolean isSdcardReady(){  
  21.         boolean sdCardExist = Environment.getExternalStorageState().equals(  
  22.                     android.os.Environment.MEDIA_MOUNTED);  
  23.         return sdCardExist;  
  24.     }  
  25.     /** 
  26.      * 获得SD路径 
  27.      * @return 
  28.      */  
  29.     public String getSdcardPath() {  
  30.         return Environment.getExternalStorageDirectory().toString()+ File.separator;  
  31.     }  
  32.       
  33.     /** 
  34.      * 获取手机该应用的私有路径 
  35.      * @param context 
  36.      * @return 
  37.      */  
  38.     public String getRomCachePath(Context context){  
  39.         return context.getCacheDir() + File.separator;  
  40.     }  
  41.       
  42.     /** 
  43.      * 检查SD卡中是否存在该文件 
  44.      * @param filePath 不包含SD卡目录的文件路径 
  45.      * @return 
  46.      */  
  47.     public boolean isSdcardFileExist(String filePath){  
  48.         File file=new File(getSdcardPath()+filePath);  
  49.         return file.exists();  
  50.     }  
  51.       
  52.     /** 
  53.      *  检查手机内存中是否存在该文件 
  54.      * @param context 
  55.      * @param fileName 不包含应用内存目录的文件路径 
  56.      * @return 
  57.      */  
  58.     public boolean isRomCacheFileExist(Context context,String filePath){  
  59.         String cachePath = getRomCachePath(context);  
  60.         File file=new File(cachePath+filePath);  
  61.         return file.exists();  
  62.     }  
  63.       
  64.     /** 
  65.      * 构建SD目录 
  66.      * @param path 不包含SD卡目录的文件全路径 
  67.      * @return 
  68.      */  
  69.     public String mkSdcardFileDirs(String path) {  
  70.         String rsPath =getSdcardPath() + path;  
  71.         File file = new File(rsPath);  
  72.         if (!file.exists())file.mkdirs();  
  73.         return rsPath;  
  74.     }  
  75.       
  76.     /** 
  77.      * 构建手机存储文件路径 
  78.      * @param context 
  79.      * @param path 不包含应用内存目录的文件全路径 
  80.      * @return 
  81.      */  
  82.     public String mkRomCacheDirs(Context context,String path) {  
  83.         String cachePath = getRomCachePath(context);  
  84.         String rsPath=cachePath+path;  
  85.         File file = new File(rsPath);  
  86.         if (!file.exists())file.mkdirs();  
  87.         return rsPath;  
  88.     }  
  89.       
  90.     /** 
  91.      * 保存图片文件 
  92.      * @param bmp 
  93.      * @param fullFilePath 保存的完整路径 
  94.      * @return 返回true如果图片保存失败或图片已存在,否则返回false 
  95.      */  
  96.     public boolean saveBitmap(Bitmap bmp,String fullFilePath) {  
  97.         if(fullFilePath==null||fullFilePath.length()<1){  
  98.             Log.e(TAG, "saveBitmap error as filePath invalid");  
  99.             return false;  
  100.         }  
  101.         FileOutputStream fos = null;  
  102.         File file = new File(fullFilePath);  
  103.         if(file.exists())return false;  
  104.         try {  
  105.             fos = new FileOutputStream(file);  
  106.             if (null != fos) {  
  107.                 bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);  
  108.                 fos.flush();  
  109.                 fos.close();  
  110.                 return true;  
  111.             }  
  112.         } catch (IOException e) {  
  113.             Log.e(TAG, "saveBitmap fail as "+e.getMessage());  
  114.         }  
  115.         return false;  
  116.     }  
  117.       
  118.     /** 
  119.      * 保存图片到SD卡 
  120.      * @param bmp 图片文件 
  121.      * @param filePath  
  122.      */  
  123.     public boolean saveBitmap2Sdcard(Bitmap bmp,String filePath) {  
  124.         if(!isSdcardReady()){  
  125.             Log.e(TAG, "saveBitmap2Sdcard error as sdCard not exist");  
  126.             return false;  
  127.         }  
  128.         return saveBitmap(bmp, getSdcardPath()+filePath);  
  129.     }  
  130.       
  131.     /** 
  132.      * 保存图片到手机内存 
  133.      * @param context 
  134.      * @param bmp 图片文件 
  135.      * @param filePath  
  136.      * @return 
  137.      */  
  138.     public boolean saveBitmap2RomCache(Context context,Bitmap bmp,String filePath){  
  139.         return saveBitmap(bmp, getRomCachePath(context)+filePath);  
  140.     }  
  141.       
  142.     /** 
  143.      * 获取图片资源 
  144.      * @param fullFilePath 
  145.      * @return 
  146.      */  
  147.     public Bitmap getBitmap(String fullFilePath){  
  148.         try {  
  149.             File file=new File(fullFilePath);  
  150.             if(file.exists()){  
  151.                 Bitmap bitmap = BitmapFactory.decodeFile(fullFilePath);  
  152.                 bitmapCaches.add(bitmap);  
  153.                 return bitmap;  
  154.             }  
  155.         } catch (Exception e) {  
  156.             Log.e(TAG, "getBitmap fail as "+e.getMessage());  
  157.         }  
  158.         return null;  
  159.     }  
  160.       
  161.     /** 
  162.      * 从SD卡获取图片资源 
  163.      * @param fullFilePath 
  164.      * @return 
  165.      */  
  166.     public Bitmap getBitmapFromSdcard(String filePath){  
  167.         return getBitmap(getSdcardPath()+filePath);  
  168.     }  
  169.       
  170.     /** 
  171.      * 从手机内存获取图片资源 
  172.      * @param fullFilePath 
  173.      * @return 
  174.      */  
  175.     public Bitmap getBitmapFromRomCache(Context context,String filePath){  
  176.         return getBitmap(getRomCachePath(context)+filePath);  
  177.     }  
  178.       
  179.     /** 
  180.      * 获得包路径下的图片文件 
  181.      * @param pkgPath 
  182.      * @param fileName 
  183.      * @return 
  184.      */  
  185.     public Bitmap getBitmapFromPackage(Class<?> clazz,String pkgPath,String fileName) {  
  186.         InputStream is = clazz.getResourceAsStream(pkgPath+fileName);  
  187.         try {  
  188.             if (null == is || is.available() <= 0)return null;  
  189.         } catch (IOException ioe) {  
  190.             Log.e(TAG, ioe.getMessage());  
  191.             try {  
  192.                 if (is != null)is.close();  
  193.             }catch (Exception e) {}  
  194.             return null;  
  195.         }  
  196.         try {  
  197.             Bitmap bitmap=BitmapFactory.decodeStream(is);  
  198.             bitmapCaches.add(bitmap);  
  199.             return bitmap;  
  200.         }catch (Exception e) {  
  201.             Log.e(TAG, "getBitmapFromPackage fail as "+e.getMessage());  
  202.         }finally {  
  203.             try {  
  204.                 if (is != null)is.close();  
  205.             }catch (Exception e) {}  
  206.         }  
  207.         return null;  
  208.     }  
  209.       
  210.     /** 
  211.      *  获得包路径下的.9.png格式图片 
  212.      * @param clazz 
  213.      * @param pkgPath 
  214.      * @param picName 
  215.      * @return 
  216.      */  
  217.     public Drawable get9pngDrawableFromPackage(  
  218.             Class<?> clazz,String pkgPath, String picName) {  
  219.         InputStream inputStream = clazz.getResourceAsStream(pkgPath + picName);  
  220.         try {  
  221.             if (null == inputStream || inputStream.available() <= 0)   
  222.                 return null;  
  223.         }   
  224.         catch (IOException e1) {  
  225.             try {  
  226.                 if (inputStream != null)   
  227.                     inputStream.close();  
  228.             }   
  229.             catch (Exception e) {}  
  230.             return null;  
  231.         }  
  232.         Bitmap bitmap = null;  
  233.         NinePatchDrawable patchy = null;  
  234.         try {  
  235.             bitmap = BitmapFactory.decodeStream(inputStream);  
  236.             bitmapCaches.add(bitmap);  
  237.             byte[] chunk = bitmap.getNinePatchChunk();  
  238.             boolean result = NinePatch.isNinePatchChunk(chunk);  
  239.             if (result) patchy = new NinePatchDrawable(bitmap, chunk, new Rect(), null);  
  240.         }   
  241.         catch (Exception e) {}   
  242.         finally {  
  243.             try {  
  244.                 if (inputStream != null)   
  245.                     inputStream.close();  
  246.             }   
  247.             catch (Exception e) {}  
  248.         }  
  249.         return patchy;  
  250.     }  
  251.   
  252.     /** 
  253.      * 释放引用的图片 
  254.      */  
  255.     public void recycleBitmaps(){  
  256.         for (int i = 0; i < bitmapCaches.size(); i++) {  
  257.             bitmapCaches.get(i).recycle();  
  258.         }  
  259.         bitmapCaches.clear();  
  260.     }  
  261.       
  262.     /** 
  263.      * 从文件中读取字符串 
  264.      * @param file 
  265.      * @return 
  266.      */  
  267.     public String readStringFile(File file){  
  268.         try {  
  269.             InputStream is = new FileInputStream(file);  
  270.             return readString(is);  
  271.         } catch (FileNotFoundException e) {  
  272.             e.printStackTrace();  
  273.         }  
  274.         return null;  
  275.     }  
  276.   
  277.     /** 
  278.      * 从文件中读取字符串 
  279.      * @param file 
  280.      * @return 
  281.      */  
  282.     public String readStringFromPackage(Class<?> clazz,String pkgPath,String fileName){  
  283.         try {  
  284.             InputStream is=clazz.getResourceAsStream(pkgPath+fileName);  
  285.             return readString(is);  
  286.         } catch (Exception e) {  
  287.             e.printStackTrace();  
  288.         }  
  289.         return null;  
  290.     }  
  291.       
  292.     private String readString(InputStream is){  
  293.         ByteArrayOutputStream bos = null;  
  294.         try{  
  295.             bos = new ByteArrayOutputStream();  
  296.             byte[] buffer = new byte[1024];  
  297.             int length = -1;  
  298.             while( (length = is.read(buffer)) != -1){  
  299.                 bos.write(buffer,0,length);  
  300.             }  
  301.             return bos.toString();   
  302.         } catch(Exception e){  
  303.             Log.e(TAG, "readStringFromPackage fail as "+e.getMessage());  
  304.         }finally{  
  305.             try {bos.close();  
  306.             } catch (IOException e) {}  
  307.             try {is.close();  
  308.             } catch (IOException e) {}  
  309.         }  
  310.         return null;  
  311.     }  
  312.       
  313.     /** 
  314.      * 从SD卡读取字符文件 
  315.      * @param filePath 
  316.      * @return 
  317.      */  
  318.     public String readStringFileFromSdcard(String filePath) {  
  319.         if(!isSdcardReady()){  
  320.             Log.e(TAG, "saveBitmap2Sdcard error as sdCard not exist");  
  321.             return null;  
  322.         }  
  323.         File file=new File(getSdcardPath()+filePath);  
  324.         return readStringFile(file);  
  325.     }  
  326.       
  327.     /** 
  328.      * 从手机内存读取字符文件 
  329.      * @param filePath 
  330.      * @return 
  331.      */  
  332.     public String readStringFileFromRomCache(Context context,String filePath) {  
  333.         File file=new File(getRomCachePath(context)+filePath);  
  334.         return readStringFile(file);  
  335.     }  
  336.       
  337.     /** 
  338.      * 存储字符文件 
  339.      * @param fullFilePath 完整路径 
  340.      * @param content 字符内容 
  341.      * @return 
  342.      */  
  343.     public boolean saveStringFile(String fullFilePath,String content){  
  344.         File file = new File(fullFilePath);  
  345.         FileOutputStream fos = null;  
  346.         try {  
  347.             fos = new FileOutputStream(file);  
  348.             fos.write(content.getBytes());  
  349.             return true;  
  350.         } catch (IOException e) {  
  351.             Log.e(TAG, "saveData fail as "+e.getMessage());  
  352.         }finally{  
  353.             try {  
  354.                 if(fos!=null) fos.close();  
  355.             } catch (IOException e) {}  
  356.         }  
  357.         return false;  
  358.     }  
  359.       
  360.     /** 
  361.      *  存储字符文件到手机内存 
  362.      * @param context 
  363.      * @param fileName 
  364.      * @param content 
  365.      * @return 
  366.      */  
  367.     public boolean saveStringFile2RomCache(Context context,String filePath,String content){  
  368.         String cachePath = getRomCachePath(context);  
  369.         return saveStringFile(cachePath+filePath, content);  
  370.     }  
  371.       
  372.     /** 
  373.      * 存储字符文件到SD卡 
  374.      * @param filePath 
  375.      * @param content 
  376.      * @return 
  377.      */  
  378.     public boolean saveStringFile2Sdcard(String filePath,String content) {  
  379.         if(!isSdcardReady()){  
  380.             Log.e(TAG, "saveStringFile2Sdcard error as sdCard not exist");  
  381.             return false;  
  382.         }  
  383.         return saveStringFile(getSdcardPath()+filePath,content);  
  384.     }  
  385. }  


[java] view plaincopy
  1. public class ResUtil {  
  2.     public static final String SD_IMG_PATH="istore/images/";  
  3.     public static final String ROM_IMG_PATH="images/";  
  4.     public static final String SD_DATA_PATH="istore/data/";  
  5.     public static final String ROM_DATA_PATH="data/";  
  6.       
  7.     public static final String PKG_PATH = "/com/linxcool/istore/";  
  8.     public static final String PKG_RES_PATH = PKG_PATH+"res/";  
  9.       
  10.     /** 
  11.      * 获得包图片资源 
  12.      * @param picName 
  13.      * @return 
  14.      */  
  15.     public static Bitmap getBitmapFromPackage(String picName){  
  16.         return ResourceManager.getInstance().getBitmapFromPackage(  
  17.                 ResUtil.class,PKG_RES_PATH, picName);  
  18.     }  
  19.       
  20.     /** 
  21.      * 获得包.9.png图片资源 
  22.      * @param picName 
  23.      * @return 
  24.      */  
  25.     public static Drawable get9pngDrawableFromPackage(String picName){  
  26.         return ResourceManager.getInstance().get9pngDrawableFromPackage(  
  27.                 ResUtil.class, PKG_RES_PATH, picName);  
  28.     }  
  29.       
  30.     /** 
  31.      * 获得SD_APK_PATH路径下的APK文件 
  32.      * @return 
  33.      */  
  34.     public static List<File> getApkFiles(){  
  35.         File filesPath=new File(  
  36.                 ResourceManager.getInstance().getSdcardPath()+SD_APK_PATH);  
  37.         if(!filesPath.exists())return null;  
  38.         File[] files=filesPath.listFiles();  
  39.         if(files==null || files.length==0)return null;  
  40.           
  41.         List<File> list=new ArrayList<File>();  
  42.         for (File file : files) {  
  43.             if(file.isDirectory())continue;  
  44.             String fileName=file.getName().toLowerCase();  
  45.             if(fileName.contains(".apk"))  
  46.                 list.add(file);  
  47.         }  
  48.         return list;  
  49.     }  
  50.       
  51.     public static String getFolderFromPath(String path) {  
  52.         String[] slipArray = path.split(File.separator);  
  53.         if( slipArray.length <= 0 )  
  54.             return null;  
  55.         int pathLen = path.length();  
  56.         int fileNameLen = slipArray[slipArray.length-1].length();  
  57.         return path.substring(0, pathLen-fileNameLen);  
  58.     }  
  59.       
  60.     public static String readStringFromPackage(String fileName){  
  61.         return ResourceManager.getInstance().readStringFromPackage(  
  62.                 ResUtil.class,PKG_RES_PATH, fileName);  
  63.     }  
  64.       
  65.     /** 
  66.      * 保存String数据 
  67.      * @param context 
  68.      * @param fileName 
  69.      * @param content 
  70.      */  
  71.     public static void saveStringData(Context context,String fileName,String content){  
  72.         if(ResourceManager.getInstance().isSdcardReady()){  
  73.             String filePath=SD_DATA_PATH+fileName;  
  74.             String folder=getFolderFromPath(filePath);  
  75.             ResourceManager.getInstance().mkSdcardFileDirs(folder);  
  76.             ResourceManager.getInstance().  
  77.             saveStringFile2Sdcard(filePath, content);  
  78.         }else{  
  79.             String filePath=ROM_DATA_PATH+fileName;  
  80.             String folder=getFolderFromPath(filePath);  
  81.             ResourceManager.getInstance().mkRomCacheDirs(context, folder);  
  82.             ResourceManager.getInstance().  
  83.             saveStringFile2RomCache(context, filePath, content);  
  84.         }  
  85.     }  
  86.       
  87.     /** 
  88.      * 读取String数据 
  89.      * @param context 
  90.      * @param fileName 
  91.      * @return 
  92.      */  
  93.     public static String readStringData(Context context,String fileName){  
  94.         if(ResourceManager.getInstance().isSdcardFileExist(SD_DATA_PATH+fileName)){  
  95.             return ResourceManager.getInstance().  
  96.             readStringFileFromSdcard(SD_DATA_PATH+fileName);  
  97.         }else if(ResourceManager.getInstance().isRomCacheFileExist(context, ROM_DATA_PATH+fileName)){  
  98.             return ResourceManager.getInstance().  
  99.             readStringFileFromRomCache(context,ROM_DATA_PATH+fileName);  
  100.         }  
  101.         return null;  
  102.     }  
  103.       
  104.     /** 
  105.      * 保存图片文件 
  106.      * @param context 
  107.      * @param bmp 
  108.      * @param fileName 
  109.      */  
  110.     public static void saveBitmap(Context context,Bitmap bmp, String fileName){  
  111.         if(ResourceManager.getInstance().isSdcardReady()){  
  112.             String filePath=SD_IMG_PATH+fileName;  
  113.             String folder=getFolderFromPath(filePath);  
  114.             ResourceManager.getInstance().mkSdcardFileDirs(folder);  
  115.             ResourceManager.getInstance().saveBitmap2Sdcard(bmp,filePath);  
  116.         }else{  
  117.             String filePath=ROM_IMG_PATH+fileName;  
  118.             String folder=getFolderFromPath(filePath);  
  119.             ResourceManager.getInstance().mkRomCacheDirs(context, folder);  
  120.             ResourceManager.getInstance().  
  121.             saveBitmap2RomCache(context, bmp, filePath);  
  122.         }  
  123.     }  
  124.       
  125.     /** 
  126.      * 获得图片文件 
  127.      * @param context 
  128.      * @param fileName 
  129.      * @return 
  130.      */  
  131.     public static Bitmap getBitmap(Context context,String fileName){  
  132.         if(ResourceManager.getInstance().isSdcardFileExist(SD_IMG_PATH+fileName)){  
  133.             return ResourceManager.getInstance().  
  134.             getBitmapFromSdcard(SD_IMG_PATH+fileName);  
  135.         }else if(ResourceManager.getInstance().isRomCacheFileExist(context, ROM_IMG_PATH+fileName)){  
  136.             return ResourceManager.getInstance().  
  137.             getBitmapFromRomCache(context,ROM_IMG_PATH+fileName);  
  138.         }  
  139.         return null;  
  140.     }  
  141.       
  142. }  



0 0
原创粉丝点击