Android Open Source - ivideo File Utils






From Project

Back to project page ivideo.

License

The source code is released under:

MIT License

If you think the Android project ivideo 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.miscellapp.ivideo.util;
//w  ww. j a  v  a2s  .  c om
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import com.miscellapp.ivideo.AppApplication;
import com.miscellapp.ivideo.FileCache;

import java.io.*;
import java.text.DecimalFormat;

/**
 * Created with IntelliJ IDEA.
 * User: chenjishi
 * Date: 12-11-18
 * Time: ????4:58
 * To change this template use File | Settings | File Templates.
 */
public class FileUtils {

    public static Object unserializeObject(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }

        ObjectInputStream ois = null;
        Object o = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            o = ois.readObject();
        } catch (Exception e) {
            file.delete();
            return null;
        } finally {
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                }
            }
        }
        return o;
    }

    public static boolean serializeObject(String path, Object o) {
        ObjectOutputStream oos = null;

        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(o);
        } catch (IOException e) {
            File file = new File(path);
            if (file.exists() && file.isFile()) {
                file.delete();
            }
            return false;
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                return false;
            }
        }

        return true;
    }

    public static void deleteFile(String path) {
        if (TextUtils.isEmpty(path)) return;

        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    public static long getCachedVideoSize(Context context) {
        String videoPath = FileCache.getVideoDirectory(context);
        return getFileSize(new File(videoPath));
    }

    public static long getFileSize(File f) {
        long size = 0;
        File flist[] = f.listFiles();
        if (flist == null) {
            return 0;
        }
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getFileSize(flist[i]);
            } else {
                size = size + flist[i].length();
            }
        }
        return size;
    }

    public static String getVideoCacheSize() {
        String cachePath = FileCache.getVideoDirectory(AppApplication.getInstance());
        return getCurrentCacheSize(cachePath);
    }

    public static String getImageCacheSize() {
        String cachePath = FileCache.getImageCacheDirectory(AppApplication.getInstance());
        return getCurrentCacheSize(cachePath);
    }

    private static String getCurrentCacheSize(String path) {
        StringBuilder sb=new StringBuilder(6);
        double size=getFileSize(new File(path));
        DecimalFormat df = new DecimalFormat();
        df.applyPattern( "0.00");
        if(size<(1024*1024)){
            sb.append(df.format(size/(1024))).append("KB");
        }else if(size<(1024*1024*1024)){
            sb.append(df.format(size/(1024*1024))).append("MB");
        }else{
            sb.append(df.format(size/(1024*1024*1024))).append("GB");
        }
        return sb.toString();
    }

    public static void clearCache() {
        String imageCachePath = FileCache.getImageCacheDirectory(AppApplication.getInstance());

        File[] cachedFiles = new File(imageCachePath).listFiles();
        if (cachedFiles == null) {
            return;
        }
        for (File f : cachedFiles) {
            f.delete();
        }
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void writeToFile(String fileName, String data) {
        BufferedWriter writer = null;

        try {
            File outFile = new File(fileName);

            writer = new BufferedWriter(new FileWriter(outFile));
            writer.write(data);
        } catch (IOException e) {

        } finally {
            try {
                if (null != writer) {
                    writer.close();
                }
            } catch (IOException e) {
            }
        }
    }

    public static String readFromFile(String filePath) {
        File inFile = new File(filePath);
        if (!inFile.exists())
            return null;

        StringBuilder sb = new StringBuilder();

        try {
            BufferedReader reader = new BufferedReader(new FileReader(inFile));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
        } catch (IOException e) {
        }

        return sb.toString();
    }
}




Java Source Code List

com.miscellapp.ivideo.AppApplication.java
com.miscellapp.ivideo.DatabaseHelper.java
com.miscellapp.ivideo.FileCache.java
com.miscellapp.ivideo.PrefsUtil.java
com.miscellapp.ivideo.activity.MainActivity.java
com.miscellapp.ivideo.activity.VideoPlayActivity2.java
com.miscellapp.ivideo.model.Video.java
com.miscellapp.ivideo.service.DownloadService.java
com.miscellapp.ivideo.util.Constants.java
com.miscellapp.ivideo.util.FileUtils.java
com.miscellapp.ivideo.util.HttpUtils.java
com.miscellapp.ivideo.util.Utils.java
com.miscellapp.ivideo.util.VideoParser.java
com.miscellapp.ivideo.util.VideoUrlParser.java
com.miscellapp.ivideo.volley.AuthFailureError.java
com.miscellapp.ivideo.volley.CacheDispatcher.java
com.miscellapp.ivideo.volley.Cache.java
com.miscellapp.ivideo.volley.DefaultRetryPolicy.java
com.miscellapp.ivideo.volley.ExecutorDelivery.java
com.miscellapp.ivideo.volley.NetworkDispatcher.java
com.miscellapp.ivideo.volley.NetworkError.java
com.miscellapp.ivideo.volley.NetworkResponse.java
com.miscellapp.ivideo.volley.Network.java
com.miscellapp.ivideo.volley.NoConnectionError.java
com.miscellapp.ivideo.volley.ParseError.java
com.miscellapp.ivideo.volley.RequestQueue.java
com.miscellapp.ivideo.volley.Request.java
com.miscellapp.ivideo.volley.ResponseDelivery.java
com.miscellapp.ivideo.volley.Response.java
com.miscellapp.ivideo.volley.RetryPolicy.java
com.miscellapp.ivideo.volley.ServerError.java
com.miscellapp.ivideo.volley.TimeoutError.java
com.miscellapp.ivideo.volley.VolleyError.java
com.miscellapp.ivideo.volley.VolleyLog.java
com.miscellapp.ivideo.volley.toolbox.AndroidAuthenticator.java
com.miscellapp.ivideo.volley.toolbox.Authenticator.java
com.miscellapp.ivideo.volley.toolbox.BasicNetwork.java
com.miscellapp.ivideo.volley.toolbox.BitmapLruCache.java
com.miscellapp.ivideo.volley.toolbox.ByteArrayPool.java
com.miscellapp.ivideo.volley.toolbox.ClearCacheRequest.java
com.miscellapp.ivideo.volley.toolbox.DiskBasedCache.java
com.miscellapp.ivideo.volley.toolbox.HttpClientStack.java
com.miscellapp.ivideo.volley.toolbox.HttpHeaderParser.java
com.miscellapp.ivideo.volley.toolbox.HttpStack.java
com.miscellapp.ivideo.volley.toolbox.HurlStack.java
com.miscellapp.ivideo.volley.toolbox.ImageLoader.java
com.miscellapp.ivideo.volley.toolbox.ImageRequest.java
com.miscellapp.ivideo.volley.toolbox.JsonArrayRequest.java
com.miscellapp.ivideo.volley.toolbox.JsonObjectRequest.java
com.miscellapp.ivideo.volley.toolbox.JsonRequest.java
com.miscellapp.ivideo.volley.toolbox.NetworkImageView.java
com.miscellapp.ivideo.volley.toolbox.NoCache.java
com.miscellapp.ivideo.volley.toolbox.PoolingByteArrayOutputStream.java
com.miscellapp.ivideo.volley.toolbox.RequestFuture.java
com.miscellapp.ivideo.volley.toolbox.StringRequest.java
com.miscellapp.ivideo.volley.toolbox.Volley.java