1.图片处理工具类
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorMatrix;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Paint;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.graphics.Bitmap.Config;
- import android.graphics.PorterDuff.Mode;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- /**
- * 处理图片的工具类.
- */
- public class imageTool {
- public static final int LEFT = 0;
- public static final int RIGHT = 1;
- public static final int TOP = 3;
- public static final int BOTTOM = 4;
- /** */
- /**
- * 图片去色,返回灰度图片
- *
- * @param bmpOriginal
- * 传入的图片
- * @return 去色后的图片
- */
- public static Bitmap toGrayscale(Bitmap bmpOriginal) {
- int width, height;
- height = bmpOriginal.getHeight();
- width = bmpOriginal.getWidth();
- Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
- Bitmap.Config.RGB_565);
- Canvas c = new Canvas(bmpGrayscale);
- Paint paint = new Paint();
- ColorMatrix cm = new ColorMatrix();
- cm.setSaturation(0);
- ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
- paint.setColorFilter(f);
- c.drawBitmap(bmpOriginal, 0, 0, paint);
- return bmpGrayscale;
- }
- /** */
- /**
- * 去色同时加圆角
- *
- * @param bmpOriginal
- * 原图
- * @param pixels
- * 圆角弧度
- * @return 修改后的图片
- */
- public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {
- return toRoundCorner(toGrayscale(bmpOriginal), pixels);
- }
- /** */
- /**
- * 把图片变成圆角
- *
- * @param bitmap
- * 需要修改的图片
- * @param pixels
- * 圆角的弧度
- * @return 圆角图片
- */
- public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
- Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
- .getHeight(), Config.ARGB_8888);
- Canvas canvas = new Canvas(output);
- final int color = 0xff424242;
- final Paint paint = new Paint();
- final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
- final RectF rectF = new RectF(rect);
- final float roundPx = pixels;
- paint.setAntiAlias(true);
- canvas.drawARGB(0, 0, 0, 0);
- paint.setColor(color);
- canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
- paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
- canvas.drawBitmap(bitmap, rect, rect, paint);
- return output;
- }
- /** */
- /**
- * 使圆角功能支持BitampDrawable
- *
- * @param bitmapDrawable
- * @param pixels
- * @return
- */
- public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable,
- int pixels) {
- Bitmap bitmap = bitmapDrawable.getBitmap();
- bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
- return bitmapDrawable;
- }
- /**
- * 读取路径中的图片,然后将其转化为缩放后的bitmap
- *
- * @param path
- */
- public static void saveBefore(String path) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- // 获取这个图片的宽和高
- Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空
- options.inJustDecodeBounds = false;
- // 计算缩放比
- int be = (int) (options.outHeight / (float) 200);
- if (be <= 0)
- be = 1;
- options.inSampleSize = 2; // 图片长宽各缩小二分之一
- // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
- bitmap = BitmapFactory.decodeFile(path, options);
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- System.out.println(w + " " + h);
- // savePNG_After(bitmap,path);
- saveJPGE_After(bitmap, path);
- }
- /**
- * 保存图片为PNG
- *
- * @param bitmap
- * @param name
- */
- public static void savePNG_After(Bitmap bitmap, String name) {
- File file = new File(name);
- try {
- FileOutputStream out = new FileOutputStream(file);
- if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
- out.flush();
- out.close();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 保存图片为JPEG
- *
- * @param bitmap
- * @param path
- */
- public static void saveJPGE_After(Bitmap bitmap, String path) {
- File file = new File(path);
- try {
- FileOutputStream out = new FileOutputStream(file);
- if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
- out.flush();
- out.close();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 水印
- *
- * @param bitmap
- * @return
- */
- public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) {
- if (src == null) {
- return null;
- }
- int w = src.getWidth();
- int h = src.getHeight();
- int ww = watermark.getWidth();
- int wh = watermark.getHeight();
- // create the new blank bitmap
- Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
- Canvas cv = new Canvas(newb);
- // draw src into
- cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
- // draw watermark into
- cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
- // save all clip
- cv.save(Canvas.ALL_SAVE_FLAG);// 保存
- // store
- cv.restore();// 存储
- return newb;
- }
- /**
- * 图片合成
- *
- * @return
- */
- public static Bitmap potoMix(int direction, Bitmap... bitmaps) {
- if (bitmaps.length <= 0) {
- return null;
- }
- if (bitmaps.length == 1) {
- return bitmaps[0];
- }
- Bitmap newBitmap = bitmaps[0];
- // newBitmap = createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction);
- for (int i = 1; i < bitmaps.length; i++) {
- newBitmap = createBitmapForFotoMix(newBitmap, bitmaps[i], direction);
- }
- return newBitmap;
- }
- private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second,
- int direction) {
- if (first == null) {
- return null;
- }
- if (second == null) {
- return first;
- }
- int fw = first.getWidth();
- int fh = first.getHeight();
- int sw = second.getWidth();
- int sh = second.getHeight();
- Bitmap newBitmap = null;
- if (direction == LEFT) {
- newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh,
- Config.ARGB_8888);
- Canvas canvas = new Canvas(newBitmap);
- canvas.drawBitmap(first, sw, 0, null);
- canvas.drawBitmap(second, 0, 0, null);
- } else if (direction == RIGHT) {
- newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh,
- Config.ARGB_8888);
- Canvas canvas = new Canvas(newBitmap);
- canvas.drawBitmap(first, 0, 0, null);
- canvas.drawBitmap(second, fw, 0, null);
- } else if (direction == TOP) {
- newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh,
- Config.ARGB_8888);
- Canvas canvas = new Canvas(newBitmap);
- canvas.drawBitmap(first, 0, sh, null);
- canvas.drawBitmap(second, 0, 0, null);
- } else if (direction == BOTTOM) {
- newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh,
- Config.ARGB_8888);
- Canvas canvas = new Canvas(newBitmap);
- canvas.drawBitmap(first, 0, 0, null);
- canvas.drawBitmap(second, 0, fh, null);
- }
- return newBitmap;
- }
- /**
- * 将Bitmap转换成指定大小
- *
- * @param bitmap
- * @param width
- * @param height
- * @return
- */
- public static Bitmap createBitmapBySize(Bitmap bitmap, int width, int height) {
- return Bitmap.createScaledBitmap(bitmap, width, height, true);
- }
- /**
- * Drawable 转 Bitmap
- *
- * @param drawable
- * @return
- */
- public static Bitmap drawableToBitmapByBD(Drawable drawable) {
- BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
- return bitmapDrawable.getBitmap();
- }
- /**
- * Bitmap 转 Drawable
- *
- * @param bitmap
- * @return
- */
- public static Drawable bitmapToDrawableByBD(Bitmap bitmap) {
- Drawable drawable = new BitmapDrawable(bitmap);
- return drawable;
- }
- /**
- * byte[] 转 bitmap
- *
- * @param b
- * @return
- */
- public static Bitmap bytesToBimap(byte[] b) {
- if (b.length != 0) {
- return BitmapFactory.decodeByteArray(b, 0, b.length);
- } else {
- return null;
- }
- }
- /**
- * bitmap 转 byte[]
- *
- * @param bm
- * @return
- */
- public static byte[] bitmapToBytes(Bitmap bm) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
- return baos.toByteArray();
- }
- }
2.获取网络状态工具类
- package com.todoo.android.app.utils;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.util.Enumeration;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- public class NetworkUtils
- {
- public static final String NET_TYPE_WIFI = "WIFI";
- public static final String NET_TYPE_MOBILE = "MOBILE";
- public static final String NET_TYPE_NO_NETWORK = "no_network";
- private Context mContext = null;
- public NetworkUtils(Context pContext) {
- this.mContext = pContext;
- }
- public static final String IP_DEFAULT = "0.0.0.0";
- public static boolean isConnectInternet(final Context pContext)
- {
- final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
- final NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
- if (networkInfo != null)
- {
- return networkInfo.isAvailable();
- }
- return false;
- }
- public static boolean isConnectWifi(final Context pContext) {
- ConnectivityManager mConnectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = mConnectivity.getActiveNetworkInfo();
- //判断网络连接类型,只有在3G或wifi里进行一些数据更新。
- int netType = -1;
- if(info != null){
- netType = info.getType();
- }
- if (netType == ConnectivityManager.TYPE_WIFI) {
- return info.isConnected();
- } else {
- return false;
- }
- }
- public static String getNetTypeName(final int pNetType)
- {
- switch (pNetType)
- {
- case 0:
- return "unknown";
- case 1:
- return "GPRS";
- case 2:
- return "EDGE";
- case 3:
- return "UMTS";
- case 4:
- return "CDMA: Either IS95A or IS95B";
- case 5:
- return "EVDO revision 0";
- case 6:
- return "EVDO revision A";
- case 7:
- return "1xRTT";
- case 8:
- return "HSDPA";
- case 9:
- return "HSUPA";
- case 10:
- return "HSPA";
- case 11:
- return "iDen";
- case 12:
- return "EVDO revision B";
- case 13:
- return "LTE";
- case 14:
- return "eHRPD";
- case 15:
- return "HSPA+";
- default:
- return "unknown";
- }
- }
- public static String getIPAddress()
- {
- try
- {
- final Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
- while (networkInterfaceEnumeration.hasMoreElements())
- {
- final NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
- final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
- while (inetAddressEnumeration.hasMoreElements())
- {
- final InetAddress inetAddress = inetAddressEnumeration.nextElement();
- if (!inetAddress.isLoopbackAddress())
- {
- return inetAddress.getHostAddress();
- }
- }
- }
- return NetworkUtils.IP_DEFAULT;
- }
- catch (final SocketException e)
- {
- return NetworkUtils.IP_DEFAULT;
- }
- }
- public String getConnTypeName() {
- ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
- if(networkInfo == null) {
- return NET_TYPE_NO_NETWORK;
- } else {
- return networkInfo.getTypeName();
- }
- }
- }
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
3.Android中资源存取工具类
- /**
- * 资源管理类
- * @author: linxcool.hu
- */
- public class ResourceManager {
- private static final String TAG="ResourceManager";
- private static ResourceManager resourceManager=new ResourceManager();
- private List<Bitmap> bitmapCaches;
- private ResourceManager(){
- bitmapCaches=new ArrayList<Bitmap>();
- }
- public static ResourceManager getInstance(){
- return resourceManager;
- }
- /**
- * 检查SD卡是否存在
- * @return 存在返回true,否则返回false
- */
- public boolean isSdcardReady(){
- boolean sdCardExist = Environment.getExternalStorageState().equals(
- android.os.Environment.MEDIA_MOUNTED);
- return sdCardExist;
- }
- /**
- * 获得SD路径
- * @return
- */
- public String getSdcardPath() {
- return Environment.getExternalStorageDirectory().toString()+ File.separator;
- }
- /**
- * 获取手机该应用的私有路径
- * @param context
- * @return
- */
- public String getRomCachePath(Context context){
- return context.getCacheDir() + File.separator;
- }
- /**
- * 检查SD卡中是否存在该文件
- * @param filePath 不包含SD卡目录的文件路径
- * @return
- */
- public boolean isSdcardFileExist(String filePath){
- File file=new File(getSdcardPath()+filePath);
- return file.exists();
- }
- /**
- * 检查手机内存中是否存在该文件
- * @param context
- * @param fileName 不包含应用内存目录的文件路径
- * @return
- */
- public boolean isRomCacheFileExist(Context context,String filePath){
- String cachePath = getRomCachePath(context);
- File file=new File(cachePath+filePath);
- return file.exists();
- }
- /**
- * 构建SD目录
- * @param path 不包含SD卡目录的文件全路径
- * @return
- */
- public String mkSdcardFileDirs(String path) {
- String rsPath =getSdcardPath() + path;
- File file = new File(rsPath);
- if (!file.exists())file.mkdirs();
- return rsPath;
- }
- /**
- * 构建手机存储文件路径
- * @param context
- * @param path 不包含应用内存目录的文件全路径
- * @return
- */
- public String mkRomCacheDirs(Context context,String path) {
- String cachePath = getRomCachePath(context);
- String rsPath=cachePath+path;
- File file = new File(rsPath);
- if (!file.exists())file.mkdirs();
- return rsPath;
- }
- /**
- * 保存图片文件
- * @param bmp
- * @param fullFilePath 保存的完整路径
- * @return 返回true如果图片保存失败或图片已存在,否则返回false
- */
- public boolean saveBitmap(Bitmap bmp,String fullFilePath) {
- if(fullFilePath==null||fullFilePath.length()<1){
- Log.e(TAG, "saveBitmap error as filePath invalid");
- return false;
- }
- FileOutputStream fos = null;
- File file = new File(fullFilePath);
- if(file.exists())return false;
- try {
- fos = new FileOutputStream(file);
- if (null != fos) {
- bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
- fos.flush();
- fos.close();
- return true;
- }
- } catch (IOException e) {
- Log.e(TAG, "saveBitmap fail as "+e.getMessage());
- }
- return false;
- }
- /**
- * 保存图片到SD卡
- * @param bmp 图片文件
- * @param filePath
- */
- public boolean saveBitmap2Sdcard(Bitmap bmp,String filePath) {
- if(!isSdcardReady()){
- Log.e(TAG, "saveBitmap2Sdcard error as sdCard not exist");
- return false;
- }
- return saveBitmap(bmp, getSdcardPath()+filePath);
- }
- /**
- * 保存图片到手机内存
- * @param context
- * @param bmp 图片文件
- * @param filePath
- * @return
- */
- public boolean saveBitmap2RomCache(Context context,Bitmap bmp,String filePath){
- return saveBitmap(bmp, getRomCachePath(context)+filePath);
- }
- /**
- * 获取图片资源
- * @param fullFilePath
- * @return
- */
- public Bitmap getBitmap(String fullFilePath){
- try {
- File file=new File(fullFilePath);
- if(file.exists()){
- Bitmap bitmap = BitmapFactory.decodeFile(fullFilePath);
- bitmapCaches.add(bitmap);
- return bitmap;
- }
- } catch (Exception e) {
- Log.e(TAG, "getBitmap fail as "+e.getMessage());
- }
- return null;
- }
- /**
- * 从SD卡获取图片资源
- * @param fullFilePath
- * @return
- */
- public Bitmap getBitmapFromSdcard(String filePath){
- return getBitmap(getSdcardPath()+filePath);
- }
- /**
- * 从手机内存获取图片资源
- * @param fullFilePath
- * @return
- */
- public Bitmap getBitmapFromRomCache(Context context,String filePath){
- return getBitmap(getRomCachePath(context)+filePath);
- }
- /**
- * 获得包路径下的图片文件
- * @param pkgPath
- * @param fileName
- * @return
- */
- public Bitmap getBitmapFromPackage(Class<?> clazz,String pkgPath,String fileName) {
- InputStream is = clazz.getResourceAsStream(pkgPath+fileName);
- try {
- if (null == is || is.available() <= 0)return null;
- } catch (IOException ioe) {
- Log.e(TAG, ioe.getMessage());
- try {
- if (is != null)is.close();
- }catch (Exception e) {}
- return null;
- }
- try {
- Bitmap bitmap=BitmapFactory.decodeStream(is);
- bitmapCaches.add(bitmap);
- return bitmap;
- }catch (Exception e) {
- Log.e(TAG, "getBitmapFromPackage fail as "+e.getMessage());
- }finally {
- try {
- if (is != null)is.close();
- }catch (Exception e) {}
- }
- return null;
- }
- /**
- * 获得包路径下的.9.png格式图片
- * @param clazz
- * @param pkgPath
- * @param picName
- * @return
- */
- public Drawable get9pngDrawableFromPackage(
- Class<?> clazz,String pkgPath, String picName) {
- InputStream inputStream = clazz.getResourceAsStream(pkgPath + picName);
- try {
- if (null == inputStream || inputStream.available() <= 0)
- return null;
- }
- catch (IOException e1) {
- try {
- if (inputStream != null)
- inputStream.close();
- }
- catch (Exception e) {}
- return null;
- }
- Bitmap bitmap = null;
- NinePatchDrawable patchy = null;
- try {
- bitmap = BitmapFactory.decodeStream(inputStream);
- bitmapCaches.add(bitmap);
- byte[] chunk = bitmap.getNinePatchChunk();
- boolean result = NinePatch.isNinePatchChunk(chunk);
- if (result) patchy = new NinePatchDrawable(bitmap, chunk, new Rect(), null);
- }
- catch (Exception e) {}
- finally {
- try {
- if (inputStream != null)
- inputStream.close();
- }
- catch (Exception e) {}
- }
- return patchy;
- }
- /**
- * 释放引用的图片
- */
- public void recycleBitmaps(){
- for (int i = 0; i < bitmapCaches.size(); i++) {
- bitmapCaches.get(i).recycle();
- }
- bitmapCaches.clear();
- }
- /**
- * 从文件中读取字符串
- * @param file
- * @return
- */
- public String readStringFile(File file){
- try {
- InputStream is = new FileInputStream(file);
- return readString(is);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 从文件中读取字符串
- * @param file
- * @return
- */
- public String readStringFromPackage(Class<?> clazz,String pkgPath,String fileName){
- try {
- InputStream is=clazz.getResourceAsStream(pkgPath+fileName);
- return readString(is);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- private String readString(InputStream is){
- ByteArrayOutputStream bos = null;
- try{
- bos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int length = -1;
- while( (length = is.read(buffer)) != -1){
- bos.write(buffer,0,length);
- }
- return bos.toString();
- } catch(Exception e){
- Log.e(TAG, "readStringFromPackage fail as "+e.getMessage());
- }finally{
- try {bos.close();
- } catch (IOException e) {}
- try {is.close();
- } catch (IOException e) {}
- }
- return null;
- }
- /**
- * 从SD卡读取字符文件
- * @param filePath
- * @return
- */
- public String readStringFileFromSdcard(String filePath) {
- if(!isSdcardReady()){
- Log.e(TAG, "saveBitmap2Sdcard error as sdCard not exist");
- return null;
- }
- File file=new File(getSdcardPath()+filePath);
- return readStringFile(file);
- }
- /**
- * 从手机内存读取字符文件
- * @param filePath
- * @return
- */
- public String readStringFileFromRomCache(Context context,String filePath) {
- File file=new File(getRomCachePath(context)+filePath);
- return readStringFile(file);
- }
- /**
- * 存储字符文件
- * @param fullFilePath 完整路径
- * @param content 字符内容
- * @return
- */
- public boolean saveStringFile(String fullFilePath,String content){
- File file = new File(fullFilePath);
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file);
- fos.write(content.getBytes());
- return true;
- } catch (IOException e) {
- Log.e(TAG, "saveData fail as "+e.getMessage());
- }finally{
- try {
- if(fos!=null) fos.close();
- } catch (IOException e) {}
- }
- return false;
- }
- /**
- * 存储字符文件到手机内存
- * @param context
- * @param fileName
- * @param content
- * @return
- */
- public boolean saveStringFile2RomCache(Context context,String filePath,String content){
- String cachePath = getRomCachePath(context);
- return saveStringFile(cachePath+filePath, content);
- }
- /**
- * 存储字符文件到SD卡
- * @param filePath
- * @param content
- * @return
- */
- public boolean saveStringFile2Sdcard(String filePath,String content) {
- if(!isSdcardReady()){
- Log.e(TAG, "saveStringFile2Sdcard error as sdCard not exist");
- return false;
- }
- return saveStringFile(getSdcardPath()+filePath,content);
- }
- }
- public class ResUtil {
- public static final String SD_IMG_PATH="istore/images/";
- public static final String ROM_IMG_PATH="images/";
- public static final String SD_DATA_PATH="istore/data/";
- public static final String ROM_DATA_PATH="data/";
- public static final String PKG_PATH = "/com/linxcool/istore/";
- public static final String PKG_RES_PATH = PKG_PATH+"res/";
- /**
- * 获得包图片资源
- * @param picName
- * @return
- */
- public static Bitmap getBitmapFromPackage(String picName){
- return ResourceManager.getInstance().getBitmapFromPackage(
- ResUtil.class,PKG_RES_PATH, picName);
- }
- /**
- * 获得包.9.png图片资源
- * @param picName
- * @return
- */
- public static Drawable get9pngDrawableFromPackage(String picName){
- return ResourceManager.getInstance().get9pngDrawableFromPackage(
- ResUtil.class, PKG_RES_PATH, picName);
- }
- /**
- * 获得SD_APK_PATH路径下的APK文件
- * @return
- */
- public static List<File> getApkFiles(){
- File filesPath=new File(
- ResourceManager.getInstance().getSdcardPath()+SD_APK_PATH);
- if(!filesPath.exists())return null;
- File[] files=filesPath.listFiles();
- if(files==null || files.length==0)return null;
- List<File> list=new ArrayList<File>();
- for (File file : files) {
- if(file.isDirectory())continue;
- String fileName=file.getName().toLowerCase();
- if(fileName.contains(".apk"))
- list.add(file);
- }
- return list;
- }
- public static String getFolderFromPath(String path) {
- String[] slipArray = path.split(File.separator);
- if( slipArray.length <= 0 )
- return null;
- int pathLen = path.length();
- int fileNameLen = slipArray[slipArray.length-1].length();
- return path.substring(0, pathLen-fileNameLen);
- }
- public static String readStringFromPackage(String fileName){
- return ResourceManager.getInstance().readStringFromPackage(
- ResUtil.class,PKG_RES_PATH, fileName);
- }
- /**
- * 保存String数据
- * @param context
- * @param fileName
- * @param content
- */
- public static void saveStringData(Context context,String fileName,String content){
- if(ResourceManager.getInstance().isSdcardReady()){
- String filePath=SD_DATA_PATH+fileName;
- String folder=getFolderFromPath(filePath);
- ResourceManager.getInstance().mkSdcardFileDirs(folder);
- ResourceManager.getInstance().
- saveStringFile2Sdcard(filePath, content);
- }else{
- String filePath=ROM_DATA_PATH+fileName;
- String folder=getFolderFromPath(filePath);
- ResourceManager.getInstance().mkRomCacheDirs(context, folder);
- ResourceManager.getInstance().
- saveStringFile2RomCache(context, filePath, content);
- }
- }
- /**
- * 读取String数据
- * @param context
- * @param fileName
- * @return
- */
- public static String readStringData(Context context,String fileName){
- if(ResourceManager.getInstance().isSdcardFileExist(SD_DATA_PATH+fileName)){
- return ResourceManager.getInstance().
- readStringFileFromSdcard(SD_DATA_PATH+fileName);
- }else if(ResourceManager.getInstance().isRomCacheFileExist(context, ROM_DATA_PATH+fileName)){
- return ResourceManager.getInstance().
- readStringFileFromRomCache(context,ROM_DATA_PATH+fileName);
- }
- return null;
- }
- /**
- * 保存图片文件
- * @param context
- * @param bmp
- * @param fileName
- */
- public static void saveBitmap(Context context,Bitmap bmp, String fileName){
- if(ResourceManager.getInstance().isSdcardReady()){
- String filePath=SD_IMG_PATH+fileName;
- String folder=getFolderFromPath(filePath);
- ResourceManager.getInstance().mkSdcardFileDirs(folder);
- ResourceManager.getInstance().saveBitmap2Sdcard(bmp,filePath);
- }else{
- String filePath=ROM_IMG_PATH+fileName;
- String folder=getFolderFromPath(filePath);
- ResourceManager.getInstance().mkRomCacheDirs(context, folder);
- ResourceManager.getInstance().
- saveBitmap2RomCache(context, bmp, filePath);
- }
- }
- /**
- * 获得图片文件
- * @param context
- * @param fileName
- * @return
- */
- public static Bitmap getBitmap(Context context,String fileName){
- if(ResourceManager.getInstance().isSdcardFileExist(SD_IMG_PATH+fileName)){
- return ResourceManager.getInstance().
- getBitmapFromSdcard(SD_IMG_PATH+fileName);
- }else if(ResourceManager.getInstance().isRomCacheFileExist(context, ROM_IMG_PATH+fileName)){
- return ResourceManager.getInstance().
- getBitmapFromRomCache(context,ROM_IMG_PATH+fileName);
- }
- return null;
- }
- }