Android开发过程中常用到的工具类HttpUtils,SDCardUtils

 在开发过程我们最长用到的就是判断网络是否可用,以及把文件存到指定目录下等等,而这如果不专门写一个工具类,将会非常的不方便,因此,我将我做项目过程中用到的几个工具类贴出来,需要用的童鞋,可以直接拿去用咯~~~

1.NetWorkUtils.java:

public class NetWorkUtils {

    /**
     * 判断网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isAvailable(Context context) {
        boolean flag = false;

        // 构建一个连接管理对象,有多个作用(WIFI, GPRS, UMTS, etc)
        // 1. 监视网络的当前状态
        // 2. 当网络发生变化时,可以提供一个通知
        // 3. 当一个已连接的网络丢失时,尝试启动另外一个网络连接
        // 4. 提供一套api来查询当前网络状态的好坏
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null) {
            flag = networkInfo.isAvailable();
        }
        return flag;
    }

    /**
     * 判断网络是否已连接
     * 
     * @param context
     * @return
     */
    public static boolean isNetWorkConnected(Context context) {
        boolean flag = false;

        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (isAvailable(context)) {
            flag = networkInfo.isConnected();
        }

        return flag;
    }

    /**
     * 判断wifi是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isWIFINetworkAvailable(Context context) {
        boolean flag = false;

        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null && networkInfo.isConnected()) {
            flag = true;
        }

        return flag;
    }

    /**
     * 判断mobile网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isMOBILENetworkAvailable(Context context) {
        boolean flag = false;

        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null && networkInfo.isConnected()) {
            flag = true;
        }

        return flag;
    }

    /**
     * 得到当前网络的类型
     * 
     * @param context
     * @return
     */
    public static int getNetworkType(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null)
            return networkInfo.getType();
        return -1;
    }

   /**
     * 判断网络是否为漫游
     */
    public static boolean isNetworkRoaming(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            Log.w(LOG_TAG, "couldn't get connectivity manager");
        } else {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null
                    && info.getType() == ConnectivityManager.TYPE_MOBILE) {
                TelephonyManager tm = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null && tm.isNetworkRoaming()) {
                    Log.d(LOG_TAG, "network is roaming");
                    return true;
                } else {
                    Log.d(LOG_TAG, "network is not roaming");
                }
            } else {
                Log.d(LOG_TAG, "not using mobile network");
            }
        }
        return false;
    }
}

2.SDCardUtils.java:

public class SDCardutils {
    public static boolean isMounted(){
        boolean flag = false;

        flag = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
        return flag;
    }
    /**
     * 获取SDCard的路径
     * @return
     */
    public static String getSDCardPath(){
        String path = null;
        if(isMounted()){
            path = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return path;
    }
    public static long getTotalSize(){
        long size = 0;
        //StatFs类是用来描述文件系统的,可以描述整个文件系统的大小
        StatFs sf = new StatFs(getSDCardPath());
        Long blockSize = sf.getBlockSizeLong();
        Long blockCount = sf.getBlockCountLong();
        size = blockCount * blockSize/1024/1024;
        return size;
    }
    public static long getAviableSize(){
        long size = 0;
        //StatFs类是用来描述文件系统的,可以描述整个文件系统的大小
        StatFs sf = new StatFs(getSDCardPath());
        Long blockSize = sf.getBlockSizeLong();
        Long blockCount = sf.getAvailableBlocksLong();
        size = blockCount * blockSize/1024/1024;
        return size;
    }
    /**
     * 获取指定公共目录的路径
     * @param type 可以包含音乐,铃声,闹钟,相册,图片,电影
     * @return
     */
    public static String getPublicPath(String type){
        String path = null;

        if(isMounted()){
            path = Environment.getExternalStoragePublicDirectory(type).getAbsolutePath();
        }
        return path;
    }
    /**
     * 获取指定用于应用程序的路径
     * @param context
     * @param type
     * @return
     */
    public static String getPrivatePath(Context context,String type){
        String path = null;
        if(isMounted()){
            path = context.getExternalFilesDir(type).getAbsolutePath();
        }
        return path;
    }
     /**
     * 将data写入type类型所指向的目录下名为filaName的文件中
     * @param data
     * @param context
     * @param fileName
     * @param type
     * @return true代表写入成功
     */
    public static boolean saveData2PrivateSDCard(byte[] data,Context context,String fileName,String type){
        boolean flag = false;
        if(isMounted()){
            File f = new File(getPrivatePath(context, type));
            if(!f.exists()){
                f.mkdirs();
            }
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(f,fileName)));
                bos.write(data,0,data.length);
                bos.flush();
                flag = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
     /**
         * 将data写入type类型的公共文件夹中名为filaname的文件中
         * @param data
         * @param context
         * @param fileName
         * @param type
         * @return true代表写入成功
         */
        public static boolean saveData2PublicSDCard(byte[] data,String fileName,String type){
            boolean flag = false;
            if(isMounted()){
                File f = new File(getPublicPath(type));
                if(!f.exists()){
                    f.mkdirs();
                }
                BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(new FileOutputStream(new File(f,fileName)));
                    bos.write(data,0,data.length);
                    bos.flush();
                    flag = true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return flag;
        }
        /**
         * 将data存入SDCard根目录下文件夹名为dir下的文件名为filaName的文件中
         * @param data
         * @param dir
         * @param fileName
         * @return
         */
        public static boolean saveData2SDCard(byte[] data,String dir,String fileName){
            boolean flag = false;
            if(isMounted()){
                String path = getSDCardPath()+File.separator+dir;
                File f = new File(path);
                if(!f.exists()){
                    f.mkdirs();
                }
            }
                BufferedOutputStream bos = null;
                try {
                    bos = new BufferedOutputStream(new FileOutputStream(new File(dir,fileName)));
                    bos.write(data,0,data.length);
                    bos.flush();
                    flag = true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            return flag;
        }
        /**
         * 读取sd卡根目录地下名为dir文件夹下的名为fileName的文件的内容
         * @param dir
         * @param fileName
         * @return
         */
        public static byte[] getDataFromSDCard(String dir,String fileName){
            byte[] data = null;
            if(isMounted()){
                String path = getSDCardPath()+File.separator+dir+File.separator+fileName;
                File f = new File(path);
                if(f.exists()){
                    BufferedInputStream bis = null;
                    ByteArrayOutputStream bos = null;

                    bos = new ByteArrayOutputStream();
                    try {
                        bis = new BufferedInputStream(new FileInputStream(f));

                        int len = 0;
                        byte[] temp = new byte[1024];
                        while((len = bis.read(temp)) != -1){
                            bos.write(temp,0,len);
                            bos.flush();
                        }
                        data = bos.toByteArray();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally{
                        try {
                            if(bis != null){
                                bis.close();
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            return data;
        }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值