转载请标明出处:http://blog.youkuaiyun.com/lmj623565791/article/details/38965311
打开大家手上的项目,基本都会有一大批的辅助类,今天特此整理出10个基本每个项目中都会使用的工具类,用于快速开发~~
在此感谢群里给我发项目中工具类的兄弟/姐妹~
1、日志工具类L.java
- package com.zhy.utils;
- import android.util.Log;
- /**
- * Log统一管理类
- *
- *
- *
- */
- public class L
- {
- private L()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函数里面初始化
- private static final String TAG = "way";
- // 下面四个是默认tag的函数
- public static void i(String msg)
- {
- if (isDebug)
- Log.i(TAG, msg);
- }
- public static void d(String msg)
- {
- if (isDebug)
- Log.d(TAG, msg);
- }
- public static void e(String msg)
- {
- if (isDebug)
- Log.e(TAG, msg);
- }
- public static void v(String msg)
- {
- if (isDebug)
- Log.v(TAG, msg);
- }
- // 下面是传入自定义tag的函数
- public static void i(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- public static void d(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- public static void e(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- public static void v(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- }
网上看到的类,注释上应该原创作者的名字,很简单的一个类;网上也有很多提供把日志记录到SDCard上的,不过我是从来没记录过,所以引入个最简单的,大家可以进行评价是否需要扩充~~
2、Toast统一管理类
- package com.zhy.utils;
- import android.content.Context;
- import android.widget.Toast;
- /**
- * Toast统一管理类
- *
- */
- public class T
- {
- private T()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- public static boolean isShow = true;
- /**
- * 短时间显示Toast
- *
- * @param context
- * @param message
- */
- public static void showShort(Context context, CharSequence message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
- }
- /**
- * 短时间显示Toast
- *
- * @param context
- * @param message
- */
- public static void showShort(Context context, int message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
- }
- /**
- * 长时间显示Toast
- *
- * @param context
- * @param message
- */
- public static void showLong(Context context, CharSequence message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_LONG).show();
- }
- /**
- * 长时间显示Toast
- *
- * @param context
- * @param message
- */
- public static void showLong(Context context, int message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_LONG).show();
- }
- /**
- * 自定义显示Toast时间
- *
- * @param context
- * @param message
- * @param duration
- */
- public static void show(Context context, CharSequence message, int duration)
- {
- if (isShow)
- Toast.makeText(context, message, duration).show();
- }
- /**
- * 自定义显示Toast时间
- *
- * @param context
- * @param message
- * @param duration
- */
- public static void show(Context context, int message, int duration)
- {
- if (isShow)
- Toast.makeText(context, message, duration).show();
- }
- }
也是非常简单的一个封装,能省则省了~~
3、SharedPreferences封装类SPUtils
- package com.zhy.utils;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Map;
- import android.content.Context;
- import android.content.SharedPreferences;
- public class SPUtils
- {
- /**
- * 保存在手机里面的文件名
- */
- public static final String FILE_NAME = "share_data";
- /**
- * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
- *
- * @param context
- * @param key
- * @param object
- */
- public static void put(Context context, String key, Object object)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- if (object instanceof String)
- {
- editor.putString(key, (String) object);
- } else if (object instanceof Integer)
- {
- editor.putInt(key, (Integer) object);
- } else if (object instanceof Boolean)
- {
- editor.putBoolean(key, (Boolean) object);
- } else if (object instanceof Float)
- {
- editor.putFloat(key, (Float) object);
- } else if (object instanceof Long)
- {
- editor.putLong(key, (Long) object);
- } else
- {
- editor.putString(key, object.toString());
- }
- SharedPreferencesCompat.apply(editor);
- }
- /**
- * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
- *
- * @param context
- * @param key
- * @param defaultObject
- * @return
- */
- public static Object get(Context context, String key, Object defaultObject)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- if (defaultObject instanceof String)
- {
- return sp.getString(key, (String) defaultObject);
- } else if (defaultObject instanceof Integer)
- {
- return sp.getInt(key, (Integer) defaultObject);
- } else if (defaultObject instanceof Boolean)
- {
- return sp.getBoolean(key, (Boolean) defaultObject);
- } else if (defaultObject instanceof Float)
- {
- return sp.getFloat(key, (Float) defaultObject);
- } else if (defaultObject instanceof Long)
- {
- return sp.getLong(key, (Long) defaultObject);
- }
- return null;
- }
- /**
- * 移除某个key值已经对应的值
- * @param context
- * @param key
- */
- public static void remove(Context context, String key)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.remove(key);
- SharedPreferencesCompat.apply(editor);
- }
- /**
- * 清除所有数据
- * @param context
- */
- public static void clear(Context context)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.clear();
- SharedPreferencesCompat.apply(editor);
- }
- /**
- * 查询某个key是否已经存在
- * @param context
- * @param key
- * @return
- */
- public static boolean contains(Context context, String key)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- return sp.contains(key);
- }
- /**
- * 返回所有的键值对
- *
- * @param context
- * @return
- */
- public static Map<String, ?> getAll(Context context)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- return sp.getAll();
- }
- /**
- * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
- *
- * @author zhy
- *
- */
- private static class SharedPreferencesCompat
- {
- private static final Method sApplyMethod = findApplyMethod();
- /**
- * 反射查找apply的方法
- *
- * @return
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- private static Method findApplyMethod()
- {
- try
- {
- Class clz = SharedPreferences.Editor.class;
- return clz.getMethod("apply");
- } catch (NoSuchMethodException e)
- {
- }
- return null;
- }
- /**
- * 如果找到则使用apply执行,否则使用commit
- *
- * @param editor
- */
- public static void apply(SharedPreferences.Editor editor)
- {
- try
- {
- if (sApplyMethod != null)
- {
- sApplyMethod.invoke(editor);
- return;
- }
- } catch (IllegalArgumentException e)
- {
- } catch (IllegalAccessException e)
- {
- } catch (InvocationTargetException e)
- {
- }
- editor.commit();
- }
- }
- }
对SharedPreference的使用做了建议的封装,对外公布出put,get,remove,clear等等方法;
注意一点,里面所有的commit操作使用了SharedPreferencesCompat.apply进行了替代,目的是尽可能的使用apply代替commit
首先说下为什么,因为commit方法是同步的,并且我们很多时候的commit操作都是UI线程中,毕竟是IO操作,尽可能异步;
所以我们使用apply进行替代,apply异步的进行写入;
但是apply相当于commit来说是new API呢,为了更好的兼容,我们做了适配;
SharedPreferencesCompat也可以给大家创建兼容类提供了一定的参考~~
4、单位转换类 DensityUtils
- package com.zhy.utils;
- import android.content.Context;
- import android.util.TypedValue;
- /**
- * 常用单位转换的辅助类
- *
- *
- *
- */
- public class DensityUtils
- {
- private DensityUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * dp转px
- *
- * @param context
- * @param val
- * @return
- */
- public static int dp2px(Context context, float dpVal)
- {
- return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
- dpVal, context.getResources().getDisplayMetrics());
- }
- /**
- * sp转px
- *
- * @param context
- * @param val
- * @return
- */
- public static int sp2px(Context context, float spVal)
- {
- return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
- spVal, context.getResources().getDisplayMetrics());
- }
- /**
- * px转dp
- *
- * @param context
- * @param pxVal
- * @return
- */
- public static float px2dp(Context context, float pxVal)
- {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (pxVal / scale);
- }
- /**
- * px转sp
- *
- * @param fontScale
- * @param pxVal
- * @return
- */
- public static float px2sp(Context context, float pxVal)
- {
- return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
- }
- }
5、SD卡相关辅助类 SDCardUtils
- package com.zhy.utils;
- import java.io.File;
- import android.os.Environment;
- import android.os.StatFs;
- /**
- * SD卡相关的辅助类
- *
- *
- *
- */
- public class SDCardUtils
- {
- private SDCardUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 判断SDCard是否可用
- *
- * @return
- */
- public static boolean isSDCardEnable()
- {
- return Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED);
- }
- /**
- * 获取SD卡路径
- *
- * @return
- */
- public static String getSDCardPath()
- {
- return Environment.getExternalStorageDirectory().getAbsolutePath()
- + File.separator;
- }
- /**
- * 获取SD卡的剩余容量 单位byte
- *
- * @return
- */
- public static long getSDCardAllSize()
- {
- if (isSDCardEnable())
- {
- StatFs stat = new StatFs(getSDCardPath());
- // 获取空闲的数据块的数量
- long availableBlocks = (long) stat.getAvailableBlocks() - 4;
- // 获取单个数据块的大小(byte)
- long freeBlocks = stat.getAvailableBlocks();
- return freeBlocks * availableBlocks;
- }
- return 0;
- }
- /**
- * 获取指定路径所在空间的剩余可用容量字节数,单位byte
- *
- * @param filePath
- * @return 容量字节 SDCard可用空间,内部存储可用空间
- */
- public static long getFreeBytes(String filePath)
- {
- // 如果是sd卡的下的路径,则获取sd卡可用容量
- if (filePath.startsWith(getSDCardPath()))
- {
- filePath = getSDCardPath();
- } else
- {// 如果是内部存储的路径,则获取内存存储的可用容量
- filePath = Environment.getDataDirectory().getAbsolutePath();
- }
- StatFs stat = new StatFs(filePath);
- long availableBlocks = (long) stat.getAvailableBlocks() - 4;
- return stat.getBlockSize() * availableBlocks;
- }
- /**
- * 获取系统存储路径
- *
- * @return
- */
- public static String getRootDirectoryPath()
- {
- return Environment.getRootDirectory().getAbsolutePath();
- }
- }
6、屏幕相关辅助类 ScreenUtils
- package com.zhy.utils;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Rect;
- import android.util.DisplayMetrics;
- import android.view.View;
- import android.view.WindowManager;
- /**
- * 获得屏幕相关的辅助类
- *
- *
- *
- */
- public class ScreenUtils
- {
- private ScreenUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 获得屏幕高度
- *
- * @param context
- * @return
- */
- public static int getScreenWidth(Context context)
- {
- WindowManager wm = (WindowManager) context
- .getSystemService(Context.WINDOW_SERVICE);
- DisplayMetrics outMetrics = new DisplayMetrics();
- wm.getDefaultDisplay().getMetrics(outMetrics);
- return outMetrics.widthPixels;
- }
- /**
- * 获得屏幕宽度
- *
- * @param context
- * @return
- */
- public static int getScreenHeight(Context context)
- {
- WindowManager wm = (WindowManager) context
- .getSystemService(Context.WINDOW_SERVICE);
- DisplayMetrics outMetrics = new DisplayMetrics();
- wm.getDefaultDisplay().getMetrics(outMetrics);
- return outMetrics.heightPixels;
- }
- /**
- * 获得状态栏的高度
- *
- * @param context
- * @return
- */
- public static int getStatusHeight(Context context)
- {
- int statusHeight = -1;
- try
- {
- Class<?> clazz = Class.forName("com.android.internal.R$dimen");
- Object object = clazz.newInstance();
- int height = Integer.parseInt(clazz.getField("status_bar_height")
- .get(object).toString());
- statusHeight = context.getResources().getDimensionPixelSize(height);
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- return statusHeight;
- }
- /**
- * 获取当前屏幕截图,包含状态栏
- *
- * @param activity
- * @return
- */
- public static Bitmap snapShotWithStatusBar(Activity activity)
- {
- View view = activity.getWindow().getDecorView();
- view.setDrawingCacheEnabled(true);
- view.buildDrawingCache();
- Bitmap bmp = view.getDrawingCache();
- int width = getScreenWidth(activity);
- int height = getScreenHeight(activity);
- Bitmap bp = null;
- bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
- view.destroyDrawingCache();
- return bp;
- }
- /**
- * 获取当前屏幕截图,不包含状态栏
- *
- * @param activity
- * @return
- */
- public static Bitmap snapShotWithoutStatusBar(Activity activity)
- {
- View view = activity.getWindow().getDecorView();
- view.setDrawingCacheEnabled(true);
- view.buildDrawingCache();
- Bitmap bmp = view.getDrawingCache();
- Rect frame = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- int statusBarHeight = frame.top;
- int width = getScreenWidth(activity);
- int height = getScreenHeight(activity);
- Bitmap bp = null;
- bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- - statusBarHeight);
- view.destroyDrawingCache();
- return bp;
- }
- }
7、App相关辅助类
- package com.zhy.utils;
- import android.content.Context;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- /**
- * 跟App相关的辅助类
- *
- *
- *
- */
- public class AppUtils
- {
- private AppUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 获取应用程序名称
- */
- public static String getAppName(Context context)
- {
- try
- {
- PackageManager packageManager = context.getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(
- context.getPackageName(), 0);
- int labelRes = packageInfo.applicationInfo.labelRes;
- return context.getResources().getString(labelRes);
- } catch (NameNotFoundException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * [获取应用程序版本名称信息]
- *
- * @param context
- * @return 当前应用的版本名称
- */
- public static String getVersionName(Context context)
- {
- try
- {
- PackageManager packageManager = context.getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(
- context.getPackageName(), 0);
- return packageInfo.versionName;
- } catch (NameNotFoundException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- }
8、软键盘相关辅助类KeyBoardUtils
- package com.zhy.utils;
- import android.content.Context;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.EditText;
- /**
- * 打开或关闭软键盘
- *
- * @author zhy
- *
- */
- public class KeyBoardUtils
- {
- /**
- * 打卡软键盘
- *
- * @param mEditText
- * 输入框
- * @param mContext
- * 上下文
- */
- public static void openKeybord(EditText mEditText, Context mContext)
- {
- InputMethodManager imm = (InputMethodManager) mContext
- .getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
- imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
- InputMethodManager.HIDE_IMPLICIT_ONLY);
- }
- /**
- * 关闭软键盘
- *
- * @param mEditText
- * 输入框
- * @param mContext
- * 上下文
- */
- public static void closeKeybord(EditText mEditText, Context mContext)
- {
- InputMethodManager imm = (InputMethodManager) mContext
- .getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
- }
- }
9、网络相关辅助类 NetUtils
- package com.zhy.utils;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- /**
- * 跟网络相关的工具类
- *
- *
- *
- */
- public class NetUtils
- {
- private NetUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 判断网络是否连接
- *
- * @param context
- * @return
- */
- public static boolean isConnected(Context context)
- {
- ConnectivityManager connectivity = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (null != connectivity)
- {
- NetworkInfo info = connectivity.getActiveNetworkInfo();
- if (null != info && info.isConnected())
- {
- if (info.getState() == NetworkInfo.State.CONNECTED)
- {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 判断是否是wifi连接
- */
- public static boolean isWifi(Context context)
- {
- ConnectivityManager cm = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm == null)
- return false;
- return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
- }
- /**
- * 打开网络设置界面
- */
- public static void openSetting(Activity activity)
- {
- Intent intent = new Intent("/");
- ComponentName cm = new ComponentName("com.android.settings",
- "com.android.settings.WirelessSettings");
- intent.setComponent(cm);
- intent.setAction("android.intent.action.VIEW");
- activity.startActivityForResult(intent, 0);
- }
- }
10、Http相关辅助类 HttpUtils
- package com.zhy.utils;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Http请求的工具类
- *
- * @author zhy
- *
- */
- public class HttpUtils
- {
- private static final int TIMEOUT_IN_MILLIONS = 5000;
- public interface CallBack
- {
- void onRequestComplete(String result);
- }
- /**
- * 异步的Get请求
- *
- * @param urlStr
- * @param callBack
- */
- public static void doGetAsyn(final String urlStr, final CallBack callBack)
- {
- new Thread()
- {
- public void run()
- {
- try
- {
- String result = doGet(urlStr);
- if (callBack != null)
- {
- callBack.onRequestComplete(result);
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- };
- }.start();
- }
- /**
- * 异步的Post请求
- * @param urlStr
- * @param params
- * @param callBack
- * @throws Exception
- */
- public static void doPostAsyn(final String urlStr, final String params,
- final CallBack callBack) throws Exception
- {
- new Thread()
- {
- public void run()
- {
- try
- {
- String result = doPost(urlStr, params);
- if (callBack != null)
- {
- callBack.onRequestComplete(result);
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- };
- }.start();
- }
- /**
- * Get请求,获得返回数据
- *
- * @param urlStr
- * @return
- * @throws Exception
- */
- public static String doGet(String urlStr)
- {
- URL url = null;
- HttpURLConnection conn = null;
- InputStream is = null;
- ByteArrayOutputStream baos = null;
- try
- {
- url = new URL(urlStr);
- conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
- conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
- conn.setRequestMethod("GET");
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- if (conn.getResponseCode() == 200)
- {
- is = conn.getInputStream();
- baos = new ByteArrayOutputStream();
- int len = -1;
- byte[] buf = new byte[128];
- while ((len = is.read(buf)) != -1)
- {
- baos.write(buf, 0, len);
- }
- baos.flush();
- return baos.toString();
- } else
- {
- throw new RuntimeException(" responseCode is not 200 ... ");
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- } finally
- {
- try
- {
- if (is != null)
- is.close();
- } catch (IOException e)
- {
- }
- try
- {
- if (baos != null)
- baos.close();
- } catch (IOException e)
- {
- }
- conn.disconnect();
- }
- return null ;
- }
- /**
- * 向指定 URL 发送POST方法的请求
- *
- * @param url
- * 发送请求的 URL
- * @param param
- * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
- * @return 所代表远程资源的响应结果
- * @throws Exception
- */
- public static String doPost(String url, String param)
- {
- PrintWriter out = null;
- BufferedReader in = null;
- String result = "";
- try
- {
- URL realUrl = new URL(url);
- // 打开和URL之间的连接
- HttpURLConnection conn = (HttpURLConnection) realUrl
- .openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- conn.setRequestProperty("charset", "utf-8");
- conn.setUseCaches(false);
- // 发送POST请求必须设置如下两行
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
- conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
- if (param != null && !param.trim().equals(""))
- {
- // 获取URLConnection对象对应的输出流
- out = new PrintWriter(conn.getOutputStream());
- // 发送请求参数
- out.print(param);
- // flush输出流的缓冲
- out.flush();
- }
- // 定义BufferedReader输入流来读取URL的响应
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
- {
- result += line;
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- // 使用finally块来关闭输出流、输入流
- finally
- {
- try
- {
- if (out != null)
- {
- out.close();
- }
- if (in != null)
- {
- in.close();
- }
- } catch (IOException ex)
- {
- ex.printStackTrace();
- }
- }
- return result;
- }
- }
如果大家在使用过程中出现什么错误,或者有更好的建议,欢迎大家留言提出~~可以不断的改进这些类~
11. Android 进度提示圆圈
protected AlertDialog mProgressDialog;
public void showDialog(){
mProgressDialog = createProgressDialog(this);
if((mProgressDialog != null) && !mProgressDialog.isShowing()){
try{
mProgressDialog.show();
LayoutInflater inflater = LayoutInflater.from(this);
View v = inflater.inflate(R.layout.xct_lthj_layout_dialog_progressbar,null);
Window window = mProgressDialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
// 设置透明度为0.3
lp.alpha = 1.0f;
// 设置暗色度
lp.dimAmount = 0.0f;
lp.gravity = Gravity.CENTER;
window.setAttributes(lp);
window.setContentView(v);
mProgressDialog.setOnKeyListener(MyKeyListener);
}catch(BadTokenException e){
// TODO check WHY!
e.printStackTrace();
}
}
}
public OnKeyListener MyKeyListener = new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
};
protected AlertDialog createProgressDialog(Context pContext) {
AlertDialog.Builder builder = new AlertDialog.Builder(pContext);
final AlertDialog progressDialog = builder.create();
progressDialog.setCanceledOnTouchOutside(false);
return progressDialog;
}
public void closeDialog(){
if(mProgressDialog != null && mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
}
12、Toast工具类2
package com.zftlive.android.tools;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.zftlive.android.MApplication;
/**
* 自定义Toast控件
* @author 曾繁添
* @version 1.0
*/
public class ToolToast {
private static Toast mToast;
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
public void run() {
mToast.cancel();
}
};
/**
* 弹出较长时间提示信息
* @param context 上下文对象
* @param msg 要显示的信息
*/
public static void showLong(Context context, String msg){
buildToast(context,msg,Toast.LENGTH_LONG).show();
}
/**
* 弹出较长时间提示信息
* @param msg 要显示的信息
*/
public static void showLong(String msg){
buildToast(MApplication.gainContext(),msg,Toast.LENGTH_LONG).show();
}
/**
* 弹出较短时间提示信息
* @param context 上下文对象
* @param msg 要显示的信息
*/
public static void showShort(Context context, String msg){
buildToast(context,msg,Toast.LENGTH_SHORT).show();
}
/**
* 弹出较短时间提示信息
* @param msg 要显示的信息
*/
public static void showShort(String msg){
buildToast(MApplication.gainContext(),msg,Toast.LENGTH_SHORT).show();
}
/**
* 构造Toast
* @param context 上下文
* @return
*/
private static Toast buildToast(Context context,String msg,int duration){
return buildToast(context,msg,duration,"#000000",16);
}
/**
* 构造Toast
* @param context 上下文
* @param msg 消息
* @param duration 显示时间
* @param bgColor 背景颜色
* @return
*/
public static Toast buildToast(Context context,String msg,int duration,String bgColor){
return buildToast(context,msg,duration,bgColor,16);
}
/**
* 构造Toast
* @param context 上下文
* @param msg 消息
* @param duration 显示时间
* @param bgColor 背景颜色
* @param textSp 文字大小
* @return
*/
public static Toast buildToast(Context context,String msg,int duration,String bgColor,int textSp){
return buildToast(context,msg,duration,bgColor,textSp,10);
}
/**
* 构造Toast
* @param context 上下文
* @param msg 消息
* @param duration 显示时间
* @param bgColor 背景颜色
* @param textSp 文字大小
* @param cornerRadius 四边圆角弧度
* @return
*/
public static Toast buildToast(Context context,String msg,int duration,String bgColor,int textSp,int cornerRadius){
mHandler.removeCallbacks(r);
if(null == mToast){
//构建Toast
mToast = Toast.makeText(context, null, duration);
mToast.setGravity(Gravity.CENTER, 0, 0);
//取消toast
mHandler.postDelayed(r, duration);
}
//设置Toast文字
TextView tv = new TextView(context);
int dpPadding = ToolUnit.dipTopx(10);
tv.setPadding(dpPadding, dpPadding, dpPadding, dpPadding);
tv.setGravity(Gravity.CENTER);
tv.setText(msg);
tv.setTextColor(Color.WHITE);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSp);
//Toast文字TextView容器
LinearLayout mLayout = new LinearLayout(context);
GradientDrawable shape = new GradientDrawable();
shape.setColor(Color.parseColor(bgColor));
shape.setCornerRadius(cornerRadius);
shape.setStroke(1, Color.parseColor(bgColor));
shape.setAlpha(180);
mLayout.setBackground(shape);
mLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
//设置layout_gravity
params.gravity = Gravity.CENTER;
mLayout.setLayoutParams(params);
//设置gravity
mLayout.setGravity(Gravity.CENTER);
mLayout.addView(tv);
//将自定义View覆盖Toast的View
mToast.setView(mLayout);
return mToast;
}
}
13、图片工具类
package com.zftlive.android.tools;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.media.ExifInterface;
import android.view.View;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.zftlive.android.config.SysEnv;
/**
* 图片工具类
* @author 曾繁添
* @version 1.0
*/
public class ToolPicture {
/**
* 截取应用程序界面(去除状态栏)
* @param activity 界面Activity
* @return Bitmap对象
*/
public static Bitmap takeScreenShot(Activity activity){
View view =activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
/**
*
*WindowManager windowMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
windowMgr.getDefaultDisplay().getMetrics(mDisplayMetrics);
return mDisplayMetrics;
*/
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,0,statusBarHeight, SysEnv.SCREEN_WIDTH, SysEnv.SCREEN_HEIGHT - statusBarHeight);
view.destroyDrawingCache();
return bitmap2;
}
/**
* 截取应用程序界面
* @param activity 界面Activity
* @return Bitmap对象
*/
public static Bitmap takeFullScreenShot(Activity activity){
activity.getWindow().getDecorView().setDrawingCacheEnabled(true);
Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache();
View view = activity.getWindow().getDecorView();
Bitmap bmp2 = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
//view.draw(new Canvas(bmp2));
//bmp就是截取的图片了,可通过bmp.compress(CompressFormat.PNG, 100, new FileOutputStream(file));把图片保存为文件。
//1、得到状态栏高度
Rect rect = new Rect();
view.getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
System.out.println("状态栏高度:" + statusBarHeight);
//2、得到标题栏高度
int wintop = activity.getWindow().findViewById(android.R.id.content).getTop();
int titleBarHeight = wintop - statusBarHeight;
System.out.println("标题栏高度:" + titleBarHeight);
// //把两个bitmap合到一起
// Bitmap bmpall=Biatmap.createBitmap(width,height,Config.ARGB_8888);
// Canvas canvas=new Canvas(bmpall);
// canvas.drawBitmap(bmp1,x,y,paint);
// canvas.drawBitmap(bmp2,x,y,paint);
return bmp;
}
/**
* 根据指定内容生成自定义宽高的二维码图片
* @param content 需要生成二维码的内容
* @param width 二维码宽度
* @param height 二维码高度
* @throws WriterException 生成二维码异常
*/
public static Bitmap makeQRImage(String content, int width, int height)
throws WriterException {
// 判断URL合法性
if (!ToolString.isNoBlankAndNoNull(content))
return null;
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 图像数据转换,使用了矩阵转换
BitMatrix bitMatrix = new QRCodeWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
// 按照二维码的算法,逐个生成二维码的图片,两个for循环是图片横列扫描的结果
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitMatrix.get(x, y))
pixels[y * width + x] = 0xff000000;
else
pixels[y * width + x] = 0xffffffff;
}
}
// 生成二维码图片的格式,使用ARGB_8888
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 读取图片属性:旋转的角度
*
* @param path 图片绝对路径
* @return degree 旋转的角度
* @throws IOException
*/
public static int gainPictureDegree(String path) throws Exception {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (Exception e) {
throw(e);
}
return degree;
}
/**
* 旋转图片
* @param angle 角度
* @param bitmap 源bitmap
* @return Bitmap 旋转角度之后的bitmap
*/
public static Bitmap rotaingBitmap(int angle,Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();;
matrix.postRotate(angle);
//重新构建Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix, true);
return resizedBitmap;
}
/**
* Drawable转成Bitmap
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
}
/**
* 从资源文件中获取图片
* @param context 上下文
* @param drawableId 资源文件id
* @return
*/
public static Bitmap gainBitmap(Context context,int drawableId){
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), drawableId);
return bmp;
}
/**
* 灰白图片(去色)
* @param bitmap 需要灰度的图片
* @return 去色之后的图片
*/
public static Bitmap toBlack(Bitmap bitmap) {
Bitmap resultBMP = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.RGB_565);
Canvas c = new Canvas(resultBMP);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
return resultBMP;
}
/**
* 将bitmap转成 byte数组
*
* @param bitmap
* @return
*/
public static byte[] toBtyeArray(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* 将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转换成指定大小
*
* @param bitmap 需要改变大小的图片
* @param width 宽
* @param height 高
* @return
*/
public static Bitmap createBitmapBySize(Bitmap bitmap, int width, int height) {
return Bitmap.createScaledBitmap(bitmap, width, height, true);
}
/**
* 在图片右下角添加水印
* @param srcBMP 原图
* @param markBMP 水印图片
* @return 合成水印后的图片
*/
public static Bitmap composeWatermark(Bitmap srcBMP, Bitmap markBMP) {
if (srcBMP == null) {
return null;
}
// 创建一个新的和SRC长度宽度一样的位图
Bitmap newb = Bitmap.createBitmap(srcBMP.getWidth(), srcBMP.getHeight(), Config.ARGB_8888);
Canvas cv = new Canvas(newb);
// 在 0,0坐标开始画入原图
cv.drawBitmap(srcBMP, 0, 0, null);
// 在原图的右下角画入水印
cv.drawBitmap(markBMP, srcBMP.getWidth() - markBMP.getWidth() + 5, srcBMP.getHeight() - markBMP.getHeight() + 5, null);
// 保存
cv.save(Canvas.ALL_SAVE_FLAG);
// 存储
cv.restore();
return newb;
}
/**
* 将图片转成指定弧度(角度)的图片
*
* @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 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(0xff424242);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 缩放图片
*
* @param bmp 需要缩放的图片源
* @param newW 需要缩放成的图片宽度
* @param newH 需要缩放成的图片高度
* @return 缩放后的图片
*/
public static Bitmap zoom(Bitmap bmp, int newW, int newH) {
// 获得图片的宽高
int width = bmp.getWidth();
int height = bmp.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newW) / width;
float scaleHeight = ((float) newH) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true);
return newbm;
}
/**
* 获得倒影的图片
* @param bitmap 原始图片
* @return 带倒影的图片
*/
public static Bitmap makeReflectionImage(Bitmap bitmap){
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height/2, width, height/2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);
Paint deafalutPaint = new Paint();
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawRect(0, height,width,height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,bitmap.getHeight(),0,bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff,0x00ffffff,TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0,height,width,bitmapWithReflection.getHeight()+reflectionGap,paint);
return bitmapWithReflection;
}
/**
* 获取验证码图片
* @param width 验证码宽度
* @param height 验证码高度
* @return 验证码Bitmap对象
*/
public synchronized static Bitmap makeValidateCode(int width, int height){
return ValidateCodeGenerator.createBitmap(width, height);
}
/**
* 获取验证码值
* @return 验证码字符串
*/
public synchronized static String gainValidateCodeValue(){
return ValidateCodeGenerator.getCode();
}
/**
* 随机生成验证码内部类
*
*/
final static class ValidateCodeGenerator{
private static final char[] CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
//default settings
private static final int DEFAULT_CODE_LENGTH = 4;
private static final int DEFAULT_FONT_SIZE = 20;
private static final int DEFAULT_LINE_NUMBER = 3;
private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 10, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 10;
private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 30;
//variables
private static String value;
private static int padding_left, padding_top;
private static Random random = new Random();
public static Bitmap createBitmap(int width,int height) {
padding_left = 0;
//创建画布
Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas c = new Canvas(bp);
//随机生成验证码字符
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < DEFAULT_CODE_LENGTH; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
value = buffer.toString();
//设置颜色
c.drawColor(Color.WHITE);
//设置画笔大小
Paint paint = new Paint();
paint.setTextSize(DEFAULT_FONT_SIZE);
for (int i = 0; i < value.length(); i++) {
//随机样式
randomTextStyle(paint);
padding_left += BASE_PADDING_LEFT + random.nextInt(RANGE_PADDING_LEFT);
padding_top = BASE_PADDING_TOP + random.nextInt(RANGE_PADDING_TOP);
c.drawText(value.charAt(i) + "", padding_left, padding_top, paint);
}
for (int i = 0; i < DEFAULT_LINE_NUMBER; i++) {
drawLine(c, paint);
}
//保存
c.save(Canvas.ALL_SAVE_FLAG);
c.restore();
return bp;
}
public static String getCode() {
return value;
}
private static void randomTextStyle(Paint paint) {
int color = randomColor(1);
paint.setColor(color);
paint.setFakeBoldText(random.nextBoolean());//true为粗体,false为非粗体
float skewX = random.nextInt(11) / 10;
skewX = random.nextBoolean() ? skewX : -skewX;
paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
paint.setUnderlineText(true); //true为下划线,false为非下划线
paint.setStrikeThruText(true); //true为删除线,false为非删除线
}
private static void drawLine(Canvas canvas, Paint paint) {
int color = randomColor(1);
int startX = random.nextInt(DEFAULT_WIDTH);
int startY = random.nextInt(DEFAULT_HEIGHT);
int stopX = random.nextInt(DEFAULT_WIDTH);
int stopY = random.nextInt(DEFAULT_HEIGHT);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
private static int randomColor(int rate) {
int red = random.nextInt(256) / rate;
int green = random.nextInt(256) / rate;
int blue = random.nextInt(256) / rate;
return Color.rgb(red, green, blue);
}
}
}
14、Properties文件 工具类
package com.zftlive.android.tools;
import java.io.InputStream;
import java.util.Properties;
import android.content.Context;
import com.zftlive.android.MApplication;
/**
* 配置文件工具类
*
* @author 曾繁添
* @version 1.0
*
*/
public final class ToolProperties extends Properties {
private static Properties property = new Properties();
public static String readAssetsProp(String fileName, String key) {
String value = "";
try {
InputStream in = MApplication.gainContext().getAssets().open(fileName);
property.load(in);
value = property.getProperty(key);
} catch (Exception e1) {
e1.printStackTrace();
}
return value;
}
public static String readAssetsProp(Context context,String fileName, String key) {
String value = "";
try {
InputStream in = context.getAssets().open(fileName);
property.load(in);
value = property.getProperty(key);
} catch (Exception e1) {
e1.printStackTrace();
}
return value;
}
public static String readAssetsProp(String fileName, String key,String defaultValue) {
String value = "";
try {
InputStream in = MApplication.gainContext().getAssets().open(fileName);
property.load(in);
value = property.getProperty(key, defaultValue);
} catch (Exception e1) {
e1.printStackTrace();
}
return value;
}
public static String readAssetsProp(Context context,String fileName, String key,String defaultValue) {
String value = "";
try {
InputStream in = context.getAssets().open(fileName);
property.load(in);
value = property.getProperty(key, defaultValue);
} catch (Exception e1) {
e1.printStackTrace();
}
return value;
}
}
15、网络工具类2:network
package com.zftlive.android.tools;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.util.Log;
/**
* 基于静态内部类实现的单例,保证线程安全的网络信息工具类 <per> 使用该工具类之前,记得在AndroidManifest.xml添加权限许可 <xmp>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* </xmp> </per>
*
* 安卓判断网络状态,只需要在相应的Activity的相关方法(onCreat/onResum)调用一行代码即可
* NetWorkUtils.getInstance(getActivity()).validateNetWork();
*
* @author 曾繁添
* @version 1.0
*/
public class ToolNetwork {
public final static String NETWORK_CMNET = "CMNET";
public final static String NETWORK_CMWAP = "CMWAP";
public final static String NETWORK_WIFI = "WIFI";
public final static String TAG = "ToolNetwork";
private static NetworkInfo networkInfo = null;
private Context mContext = null;
private ToolNetwork() {
}
public static ToolNetwork getInstance() {
return SingletonHolder.instance;
}
public ToolNetwork init(Context context){
this.mContext = context;
return this;
}
/**
* 判断网络是否可用
*
* @return 是/否
*/
public boolean isAvailable() {
ConnectivityManager manager = (ConnectivityManager) mContext
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if (null == manager) {
return false;
}
networkInfo = manager.getActiveNetworkInfo();
if (null == networkInfo || !networkInfo.isAvailable()) {
return false;
}
return true;
}
/**
* 判断网络是否已连接
*
* @return 是/否
*/
public boolean isConnected() {
if (!isAvailable()) {
return false;
}
if (!networkInfo.isConnected()) {
return false;
}
return true;
}
/**
* 检查当前环境网络是否可用,不可用跳转至开启网络界面,不设置网络强制关闭当前Activity
*/
public void validateNetWork() {
if (!isConnected()) {
Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setTitle("网络设置");
dialogBuilder.setMessage("网络不可用,是否现在设置网络?");
dialogBuilder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((Activity) mContext).startActivityForResult(
new Intent(
Settings.ACTION_SETTINGS),
which);
}
});
dialogBuilder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogBuilder.create();
dialogBuilder.show();
}
}
/**
* 获取网络连接信息</br> 无网络:</br> WIFI网络:WIFI</br> WAP网络:CMWAP</br>
* NET网络:CMNET</br>
*
* @return
*/
public String getNetworkType() {
if (isConnected()) {
int type = networkInfo.getType();
if (ConnectivityManager.TYPE_MOBILE == type) {
Log.i(TAG,
"networkInfo.getExtraInfo()-->"
+ networkInfo.getExtraInfo());
if (NETWORK_CMWAP.equals(networkInfo.getExtraInfo()
.toLowerCase())) {
return NETWORK_CMWAP;
} else {
return NETWORK_CMNET;
}
} else if (ConnectivityManager.TYPE_WIFI == type) {
return NETWORK_WIFI;
}
}
return "";
}
private static class SingletonHolder {
private static ToolNetwork instance = new ToolNetwork();
}
}
16、调用系统服务(打电话,发短信,获取相册,调用浏览器。。。)
package com.zftlive.android.tools;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
/**
* 手机相关操作API
* @author 曾繁添
* @version 1.0
*
*/
public class ToolPhone {
/**
* 直接呼叫指定的号码(需要<uses-permission android:name="android.permission.CALL_PHONE"/>权限)
* @param mContext 上下文Context
* @param phoneNumber 需要呼叫的手机号码
*/
public static void callPhone(Context mContext,String phoneNumber){
Uri uri = Uri.parse("tel:" + phoneNumber);
Intent call = new Intent(Intent.ACTION_CALL, uri);
mContext.startActivity(call);
}
/**
* 跳转至拨号界面
* @param mContext 上下文Context
* @param phoneNumber 需要呼叫的手机号码
*/
public static void toCallPhoneActivity(Context mContext,String phoneNumber){
Uri uri = Uri.parse("tel:" + phoneNumber);
Intent call = new Intent(Intent.ACTION_DIAL, uri);
mContext.startActivity(call);
}
/**
* 直接调用短信API发送信息(设置监听发送和接收状态)
* @param strPhone 手机号码
* @param strMsgContext 短信内容
*/
public static void sendMessage(final Context mContext,final String strPhone,final String strMsgContext){
//处理返回的发送状态
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sendIntent= PendingIntent.getBroadcast(mContext, 0, sentIntent,0);
// register the Broadcast Receivers
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext,"短信发送成功", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
//处理返回的接收状态
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent backIntent= PendingIntent.getBroadcast(mContext, 0,deliverIntent, 0);
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
Toast.makeText(mContext,strPhone+"已经成功接收", Toast.LENGTH_SHORT).show();
}
},
new IntentFilter(DELIVERED_SMS_ACTION)
);
//拆分短信内容(手机短信长度限制)
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> msgList = smsManager.divideMessage(strMsgContext);
for (String text : msgList) {
smsManager.sendTextMessage(strPhone, null, text, sendIntent, backIntent);
}
}
/**
* 跳转至发送短信界面(自动设置接收方的号码)
* @param mActivity Activity
* @param strPhone 手机号码
* @param strMsgContext 短信内容
*/
public static void toSendMessageActivity(Context mContext,String strPhone,String strMsgContext){
if(PhoneNumberUtils.isGlobalPhoneNumber(strPhone)){
Uri uri = Uri.parse("smsto:" + strPhone);
Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
sendIntent.putExtra("sms_body", strMsgContext);
mContext.startActivity(sendIntent);
}
}
/**
* 跳转至联系人选择界面
* @param mContext 上下文
* @param requestCode 请求返回区分代码
*/
public static void toChooseContactsList(Activity mContext,int requestCode){
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
mContext.startActivityForResult(intent, requestCode);
}
/**
* 获取选择的联系人的手机号码
* @param mContext 上下文
* @param resultCode 请求返回Result状态区分代码
* @param data onActivityResult返回的Intent
* @return
*/
public static String getChoosedPhoneNumber(Activity mContext,int resultCode,Intent data) {
//返回结果
String phoneResult = "";
if (Activity.RESULT_OK == resultCode)
{
Uri uri = data.getData();
Cursor mCursor = mContext.managedQuery(uri, null, null, null, null);
mCursor.moveToFirst();
int phoneColumn = mCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = mCursor.getInt(phoneColumn);
if (phoneNum > 0) {
// 获得联系人的ID号
int idColumn = mCursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = mCursor.getString(idColumn);
// 获得联系人的电话号码的cursor;
Cursor phones = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
if (phones.moveToFirst()) {
// 遍历所有的电话号码
for (; !phones.isAfterLast(); phones.moveToNext()) {
int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phones.getInt(typeindex);
String phoneNumber = phones.getString(index);
switch (phone_type) {
case 2:
phoneResult = phoneNumber;
break;
}
}
if (!phones.isClosed()) {
phones.close();
}
}
}
//关闭游标
mCursor.close();
}
return phoneResult;
}
/**
* 跳转至拍照程序界面
* @param mContext 上下文
* @param requestCode 请求返回Result区分代码
*/
public static void toCameraActivity(Activity mContext,int requestCode){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mContext.startActivityForResult(intent, requestCode);
}
/**
* 跳转至相册选择界面
* @param mContext 上下文
* @param requestCode
*/
public static void toImagePickerActivity(Activity mContext,int requestCode){
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
mContext.startActivityForResult(intent, requestCode);
}
/**
* 获得选中相册的图片
* @param mContext 上下文
* @param data onActivityResult返回的Intent
* @return
*/
public static Bitmap getChoosedImage(Activity mContext,Intent data){
if (data == null) {
return null;
}
Bitmap bm = null;
// 外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
ContentResolver resolver = mContext.getContentResolver();
// 此处的用于判断接收的Activity是不是你想要的那个
try {
Uri originalUri = data.getData(); // 获得图片的uri
bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); // 显得到bitmap图片
// 这里开始的第二部分,获取图片的路径:
String[] proj = {
MediaStore.Images.Media.DATA
};
// 好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = mContext.managedQuery(originalUri, proj, null, null, null);
// 按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
// 最后根据索引值获取图片路径
String path = cursor.getString(column_index);
//不用了关闭游标
cursor.close();
} catch (Exception e) {
Log.e("ToolPhone", e.getMessage());
}
return bm;
}
/**
* 调用本地浏览器打开一个网页
* @param mContext 上下文
* @param strSiteUrl 网页地址
*/
public static void openWebSite(Context mContext,String strSiteUrl){
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strSiteUrl));
mContext.startActivity(webIntent);
}
/**
* 跳转至系统设置界面
* @param mContext 上下文
*/
public static void toSettingActivity(Context mContext){
Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
mContext.startActivity(settingsIntent);
}
/**
* 跳转至WIFI设置界面
* @param mContext 上下文
*/
public static void toWIFISettingActivity(Context mContext){
Intent wifiSettingsIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
mContext.startActivity(wifiSettingsIntent);
}
/**
* 启动本地应用打开PDF
* @param mContext 上下文
* @param filePath 文件路径
*/
public static void openPDFFile(Context mContext, String filePath) {
try {
File file = new File(filePath);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
} catch (Exception e) {
Toast.makeText(mContext, "未检测到可打开PDF相关软件", Toast.LENGTH_SHORT)
.show();
}
}
/**
* 启动本地应用打开PDF
* @param mContext 上下文
* @param filePath 文件路径
*/
public static void openWordFile(Context mContext, String filePath) {
try {
File file = new File(filePath);
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(path, "application/msword");
mContext.startActivity(intent);
}
} catch (Exception e) {
Toast.makeText(mContext, "未检测到可打开Word文档相关软件", Toast.LENGTH_SHORT)
.show();
}
}
/**
* 调用WPS打开office文档
* http://bbs.wps.cn/thread-22349340-1-1.html
* @param mContext 上下文
* @param filePath 文件路径
*/
public static void openOfficeByWPS(Context mContext, String filePath){
try {
//文件存在性检查
File file = new File(filePath);
if (!file.exists()) {
Toast.makeText(mContext, filePath+"文件路径不存在", Toast.LENGTH_SHORT).show();
return;
}
//检查是否安装WPS
String wpsPackageEng = "cn.wps.moffice_eng";//普通版与英文版一样
// String wpsActivity = "cn.wps.moffice.documentmanager.PreStartActivity";
String wpsActivity2 = "cn.wps.moffice.documentmanager.PreStartActivity2";//默认第三方程序启动
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setClassName(wpsPackageEng,wpsActivity2);
Uri uri = Uri.fromFile(new File(filePath));
intent.setData(uri);
mContext.startActivity(intent);
}catch (ActivityNotFoundException e){
Toast.makeText(mContext, "本地未安装WPS", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(mContext, "打开文档失败", Toast.LENGTH_SHORT).show();
}
}
/**
* 判断是否安装指定包名的APP
* @param mContext 上下文
* @param packageName 包路径
* @return
*/
public static boolean isInstalledApp(Context mContext, String packageName) {
if (packageName == null || "".equals(packageName)) {
return false;
}
try {
ApplicationInfo info = mContext.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
/**
* 判断是否存在指定的Activity
* @param mContext 上下文
* @param packageName 包名
* @param className activity全路径类名
* @return
*/
public static boolean isExistActivity(Context mContext, String packageName,String className) {
Boolean result = true;
Intent intent = new Intent();
intent.setClassName(packageName, className);
if (mContext.getPackageManager().resolveActivity(intent, 0) == null) {
result = false;
} else if (intent.resolveActivity(mContext.getPackageManager()) == null) {
result = false;
} else {
List<ResolveInfo> list = mContext.getPackageManager()
.queryIntentActivities(intent, 0);
if (list.size() == 0) {
result = false;
}
}
return result;
}
}
17、文件操作工具 包括sdcard等
package com.zftlive.android.tools;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
/**
* 文件工具类
*
* @author 曾繁添
* @version 1.0
*/
public class ToolFile {
private static final String TAG = ToolFile.class.getSimpleName();
/**
* 检查是否已挂载SD卡镜像(是否存在SD卡)
*
* @return
*/
public static boolean isMountedSDCard() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true;
} else {
Log.w(TAG, "SDCARD is not MOUNTED !");
return false;
}
}
/**
* 获取SD卡剩余容量(单位Byte)
*
* @return
*/
public static long gainSDFreeSize() {
if (isMountedSDCard()) {
// 取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// 获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSize();
// 空闲的数据块的数量
long freeBlocks = sf.getAvailableBlocks();
// 返回SD卡空闲大小
return freeBlocks * blockSize; // 单位Byte
} else {
return 0;
}
}
/**
* 获取SD卡总容量(单位Byte)
*
* @return
*/
public static long gainSDAllSize() {
if (isMountedSDCard()) {
// 取得SD卡文件路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
// 获取单个数据块的大小(Byte)
long blockSize = sf.getBlockSize();
// 获取所有数据块数
long allBlocks = sf.getBlockCount();
// 返回SD卡大小(Byte)
return allBlocks * blockSize;
} else {
return 0;
}
}
/**
* 获取可用的SD卡路径(若SD卡不没有挂载则返回"")
*
* @return
*/
public static String gainSDCardPath() {
if (isMountedSDCard()) {
File sdcardDir = Environment.getExternalStorageDirectory();
if (!sdcardDir.canWrite()) {
Log.w(TAG, "SDCARD can not write !");
}
return sdcardDir.getPath();
}
return "";
}
/**
* 以行为单位读取文件内容,一次读一整行,常用于读面向行的格式化文件
* @param filePath 文件路径
*/
public static String readFileByLines(String filePath) throws IOException
{
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try
{
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),System.getProperty("file.encoding")));
String tempString = null;
while ((tempString = reader.readLine()) != null)
{
sb.append(tempString);
sb.append("\n");
}
reader.close();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (reader != null){reader.close();}
}
return sb.toString();
}
/**
* 以行为单位读取文件内容,一次读一整行,常用于读面向行的格式化文件
* @param filePath 文件路径
* @param encoding 写文件编码
*/
public static String readFileByLines(String filePath,String encoding) throws IOException
{
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try
{
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),encoding));
String tempString = null;
while ((tempString = reader.readLine()) != null)
{
sb.append(tempString);
sb.append("\n");
}
reader.close();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (reader != null){reader.close();}
}
return sb.toString();
}
/**
* 保存内容
* @param filePath 文件路径
* @param content 保存的内容
* @throws IOException
*/
public static void saveToFile(String filePath,String content) throws IOException
{
saveToFile(filePath,content,System.getProperty("file.encoding"));
}
/**
* 指定编码保存内容
* @param filePath 文件路径
* @param content 保存的内容
* @param encoding 写文件编码
* @throws IOException
*/
public static void saveToFile(String filePath,String content,String encoding) throws IOException
{
BufferedWriter writer = null;
File file = new File(filePath);
try
{
if(!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), encoding));
writer.write(content);
} finally
{
if (writer != null){writer.close();}
}
}
/**
* 追加文本
* @param content 需要追加的内容
* @param file 待追加文件源
* @throws IOException
*/
public static void appendToFile(String content, File file) throws IOException
{
appendToFile(content, file, System.getProperty("file.encoding"));
}
/**
* 追加文本
* @param content 需要追加的内容
* @param file 待追加文件源
* @param encoding 文件编码
* @throws IOException
*/
public static void appendToFile(String content, File file, String encoding) throws IOException
{
BufferedWriter writer = null;
try
{
if(!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding));
writer.write(content);
} finally
{
if (writer != null){writer.close();}
}
}
/**
* 判断文件是否存在
* @param filePath 文件路径
* @return 是否存在
* @throws Exception
*/
public static Boolean isExsit(String filePath)
{
Boolean flag = false ;
try
{
File file = new File(filePath);
if(file.exists())
{
flag = true;
}
}catch(Exception e){
System.out.println("判断文件失败-->"+e.getMessage());
}
return flag;
}
/**
* 快速读取程序应用包下的文件内容
*
* @param context
* 上下文
* @param filename
* 文件名称
* @return 文件内容
* @throws IOException
*/
public static String read(Context context, String filename) throws IOException {
FileInputStream inStream = context.openFileInput(filename);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
return new String(data);
}
/**
* 读取指定目录文件的文件内容
*
* @param fileName
* 文件名称
* @return 文件内容
* @throws Exception
*/
public static String read(String fileName) throws IOException {
FileInputStream inStream = new FileInputStream(fileName);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
return new String(data);
}
/***
* 以行为单位读取文件内容,一次读一整行,常用于读面向行的格式化文件
*
* @param fileName
* 文件名称
* @param encoding
* 文件编码
* @return 字符串内容
* @throws IOException
*/
public static String read(String fileName, String encoding)
throws IOException {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), encoding));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return sb.toString();
}
/**
* 读取raw目录的文件内容
*
* @param context
* 内容上下文
* @param rawFileId
* raw文件名id
* @return
*/
public static String readRawValue(Context context, int rawFileId) {
String result = "";
try {
InputStream is = context.getResources().openRawResource(rawFileId);
int len = is.available();
byte[] buffer = new byte[len];
is.read(buffer);
result = new String(buffer, "UTF-8");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 读取assets目录的文件内容
*
* @param context
* 内容上下文
* @param fileName
* 文件名称,包含扩展名
* @return
*/
public static String readAssetsValue(Context context, String fileName) {
String result = "";
try {
InputStream is = context.getResources().getAssets().open(fileName);
int len = is.available();
byte[] buffer = new byte[len];
is.read(buffer);
result = new String(buffer, "UTF-8");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 读取assets目录的文件内容
*
* @param context
* 内容上下文
* @param fileName
* 文件名称,包含扩展名
* @return
*/
public static List<String> readAssetsListValue(Context context, String fileName) {
List<String> list = new ArrayList<String>();
try {
InputStream in = context.getResources().getAssets().open(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String str = null;
while ((str = br.readLine()) != null) {
list.add(str);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* 获取SharedPreferences文件内容
*
* @param context
* 上下文
* @param fileNameNoExt
* 文件名称(不用带后缀名)
* @return
*/
public static Map<String, ?> readShrePerface(Context context,String fileNameNoExt) {
SharedPreferences preferences = context.getSharedPreferences(
fileNameNoExt, Context.MODE_PRIVATE);
return preferences.getAll();
}
/**
* 写入SharedPreferences文件内容
*
* @param context
* 上下文
* @param fileNameNoExt
* 文件名称(不用带后缀名)
* @param values
* 需要写入的数据Map(String,Boolean,Float,Long,Integer)
* @return
*/
public static void writeShrePerface(Context context, String fileNameNoExt,Map<String, ?> values) {
try {
SharedPreferences preferences = context.getSharedPreferences(fileNameNoExt, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
for (Iterator iterator = values.entrySet().iterator(); iterator.hasNext();)
{
Map.Entry<String, ?> entry = (Map.Entry<String, ?>) iterator.next();
if (entry.getValue() instanceof String) {
editor.putString(entry.getKey(), (String) entry.getValue());
} else if (entry.getValue() instanceof Boolean) {
editor.putBoolean(entry.getKey(),(Boolean) entry.getValue());
} else if (entry.getValue() instanceof Float) {
editor.putFloat(entry.getKey(), (Float) entry.getValue());
} else if (entry.getValue() instanceof Long) {
editor.putLong(entry.getKey(), (Long) entry.getValue());
} else if (entry.getValue() instanceof Integer) {
editor.putInt(entry.getKey(),(Integer) entry.getValue());
}
}
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写入应用程序包files目录下文件
*
* @param context
* 上下文
* @param fileName
* 文件名称
* @param content
* 文件内容
*/
public static void write(Context context, String fileName, String content) {
try {
FileOutputStream outStream = context.openFileOutput(fileName,
Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写入应用程序包files目录下文件
*
* @param context
* 上下文
* @param fileName
* 文件名称
* @param content
* 文件内容
*/
public static void write(Context context, String fileName, byte[] content) {
try {
FileOutputStream outStream = context.openFileOutput(fileName,
Context.MODE_PRIVATE);
outStream.write(content);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写入应用程序包files目录下文件
*
* @param context
* 上下文
* @param fileName
* 文件名称
* @param modeType
* 文件写入模式(Context.MODE_PRIVATE、Context.MODE_APPEND、Context.
* MODE_WORLD_READABLE、Context.MODE_WORLD_WRITEABLE)
* @param content
* 文件内容
*/
public static void write(Context context, String fileName, byte[] content,
int modeType) {
try {
FileOutputStream outStream = context.openFileOutput(fileName,
modeType);
outStream.write(content);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 指定编码将内容写入目标文件
*
* @param target
* 目标文件
* @param content
* 文件内容
* @param encoding
* 写入文件编码
* @throws Exception
*/
public static void write(File target, String content, String encoding)
throws IOException {
BufferedWriter writer = null;
try {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(target, false), encoding));
writer.write(content);
} finally {
if (writer != null) {
writer.close();
}
}
}
/**
* 指定目录写入文件内容
* @param filePath 文件路径+文件名
* @param content 文件内容
* @throws IOException
*/
public static void write(String filePath, byte[] content)
throws IOException {
FileOutputStream fos = null;
try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
fos.write(content);
fos.flush();
} finally {
if (fos != null) {
fos.close();
}
}
}
/**
* 写入文件
*
* @param inputStream下载文件的字节流对象
* @param filePath文件的存放路径(带文件名称)
* @throws IOException
*/
public static File write(InputStream inputStream, String filePath) throws IOException {
OutputStream outputStream = null;
// 在指定目录创建一个空文件并获取文件对象
File mFile = new File(filePath);
if (!mFile.getParentFile().exists())
mFile.getParentFile().mkdirs();
try {
outputStream = new FileOutputStream(mFile);
byte buffer[] = new byte[4 * 1024];
int lenght = 0 ;
while ((lenght = inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,lenght);
}
outputStream.flush();
return mFile;
} catch (IOException e) {
Log.e(TAG, "写入文件失败,原因:"+e.getMessage());
throw e;
}finally{
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
}
}
}
/**
* 指定目录写入文件内容
* @param filePath 文件路径+文件名
* @param content 文件内容
* @throws IOException
*/
public static void saveAsJPEG(Bitmap bitmap,String filePath)
throws IOException {
FileOutputStream fos = null;
try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,fos);
fos.flush();
} finally {
if (fos != null) {
fos.close();
}
}
}
/**
* 指定目录写入文件内容
* @param filePath 文件路径+文件名
* @param content 文件内容
* @throws IOException
*/
public static void saveAsPNG(Bitmap bitmap,String filePath)
throws IOException {
FileOutputStream fos = null;
try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100,fos);
fos.flush();
} finally {
if (fos != null) {
fos.close();
}
}
}
}