Android Open Source - Avatar File Size Util






From Project

Back to project page Avatar.

License

The source code is released under:

GNU General Public License

If you think the Android project Avatar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.syw.avatar.util;
/*from  w w w. ja  v a  2 s .c o m*/
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;

public class FileSizeUtil {
    public static final int UNIT_B  = 1;// ???????????????B?double?
    public static final int UNIT_KB = 2;// ???????????????KB?double?
    public static final int UNIT_MB = 3;// ???????????????MB?double?
    public static final int UNIT_GB = 4;// ???????????????GB?double?

    /**
     * ??????????????????????
     * 
     * @param filePath
     *            ????
     * @param sizeType
     *            ??????????1?B??2?KB??3?MB??4?GB
     * @return double?????
     */
    public static double getFileOrFilesSize(String filePath, int sizeType) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            SLog.e("?????????", "??????!");
        }
        return FormetFileSize(blockSize, sizeType);
    }

    /**
     * ???????????????????????
     * 
     * @param filePath
     *            ????
     * @return ?????B??KB??MB??GB????
     */
    public static String getAutoFileOrFilesSize(String filePath) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            SLog.e("?????????", "??????!");
        }
        return FormetFileSize(blockSize);
    }

    /**
     * ???????????
     * 
     * @param f
     * @return
     * @throws Exception
     */
    private static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
            fis.close();
        } else {
            file.createNewFile();
            SLog.e("?????????", "??????!");
        }
        return size;
    }

    /**
     * ?????????
     * 
     * @param f
     * @return
     * @throws Exception
     */
    private static long getFileSizes(File f) throws Exception {
        long size = 0;
        File flist[] = f.listFiles();
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getFileSizes(flist[i]);
            } else {
                size = size + getFileSize(flist[i]);
            }
        }
        return size;
    }

    /**
     * ?????????
     * 
     * @param fileS
     * @return
     */
    public static String FormetFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }

    /**
     * ?????????
     * 
     * @param fileS
     * @return
     */
    public static String FormetFileSize(long fileS, String decialFormat) {
        DecimalFormat df = null;
        if (decialFormat != null && decialFormat.contains("#")){
            df = new DecimalFormat(decialFormat);
        }
        else{
            df = new DecimalFormat("#.00");
        }
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }

    /**
     * ?????????,?????????
     * 
     * @param fileS
     * @param sizeType
     * @return
     */
    private static double FormetFileSize(long fileS, int sizeType) {
        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
        case UNIT_B:
            fileSizeLong = Double.valueOf(df.format((double) fileS));
            break;
        case UNIT_KB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
            break;
        case UNIT_MB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
            break;
        case UNIT_GB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
            break;
        default:
            break;
        }
        return fileSizeLong;
    }
}




Java Source Code List

com.syw.avatar.AlbumAdapter.java
com.syw.avatar.AlbumFragment.java
com.syw.avatar.AlbumInfo.java
com.syw.avatar.AlbumSerializable.java
com.syw.avatar.AvatarApplication.java
com.syw.avatar.Constants.java
com.syw.avatar.CropperActivity.java
com.syw.avatar.LocalPhotoActivity.java
com.syw.avatar.MainActivity.java
com.syw.avatar.PhotoInfo.java
com.syw.avatar.PhotoPickerAdapter.java
com.syw.avatar.PhotoPickerFragment.java
com.syw.avatar.PhotoSerializable.java
com.syw.avatar.util.FileSizeUtil.java
com.syw.avatar.util.ImageUtil.java
com.syw.avatar.util.SLog.java
com.syw.avatar.util.ThumbnailsUtil.java
com.syw.avatar.widget.ClipImageView.java
com.syw.avatar.widget.ClipView.java