generate File Size in readable string - Android java.io

Android examples for java.io:File Size

Description

generate File Size in readable string

Demo Code

import android.text.TextPaint;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

public class Main{

    public static String generateFileSize(long size) {
        String fileSize;/*from   w ww. j  a v  a 2 s.  c  om*/
        if (size < KB)
            fileSize = size + "B";
        else if (size < MB)
            fileSize = String.format("%.1f", size / KB) + "KB";
        else if (size < GB)
            fileSize = String.format("%.1f", size / MB) + "MB";
        else
            fileSize = String.format("%.1f", size / GB) + "GB";

        return fileSize;
    }

}

Related Tutorials