Android Open Source - VideoExtand File Util






From Project

Back to project page VideoExtand.

License

The source code is released under:

Apache License

If you think the Android project VideoExtand 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

/*
 * Copyright (C) 2014 The Android Open Source Project
 */*from ww w  . j a va2  s. com*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Auther?yinglovezhuzhu@gmail.com
 * File: FileUtil.java
 * Date?2014?1?2?
 * Version?v1.0
 */  
package com.yuninfo.videoextand.utils;

import java.io.File;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.Formatter;

/**
 * ???
 * @author yinglovezhuzhu@gmail.com
 */
public class FileUtil {
  
  private FileUtil() {}
  
  /**
   * Clean cache directory
   * @param context
   */
  public static void cleanInternalCache(Context context) {
    deleteFile(context.getCacheDir());
  }

  /**
   * Clean database directory
   * @param context
   */
  public static void cleanDatabases(Context context) {
    String [] databases = context.databaseList();
    if(databases != null) {
      for (String database : databases) {
        context.deleteDatabase(database);
      }
    }
  }
  
  /**
   * Delete database directory
   * @param context
   */
  public static void deleteDatabasesDir(Context context) {
    deleteFile(new File(context.getApplicationInfo().dataDir + "/databases"));
  }

  /**
   * Clean shared_prefs directory
   * @param context
   */
  public static void cleanSharedPreference(Context context) {
    deleteFile(new File(context.getApplicationInfo().dataDir + "/shared_prefs"));
  }
  
  /**
   * Delete database by name.
   * @param context
   * @param dbName
   */
  public static void cleanDatabaseByName(Context context, String dbName) {
    context.deleteDatabase(dbName);
  }
  
  

  /**
   * Clean files directory
   * @param context
   */
  public static void cleanFiles(Context context) {
    deleteFile(context.getFilesDir());
  }

  /**
   * Clean external cache directory.
   * @param context
   */
  @SuppressLint("NewApi")
  public static void cleanExternalCache(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      deleteFile(context.getExternalCacheDir());
    }
  }

  /**
   * Clean all data
   * @param context
   * @param filepath
   */
  public static void cleanApplicationData(Context context, String... filepath) {
    cleanInternalCache(context);
    cleanExternalCache(context);
    cleanDatabases(context);
    cleanSharedPreference(context);
    cleanFiles(context);
    for(String file : filepath) {
      deleteFile(new File(file));
    }
  }

  /**
   * Delete file(include not empty directory)
   * @param file
   */
  public static void deleteFile(File file) {
    if(file.exists()) {
      if(file.isDirectory()) {
        File [] files = file.listFiles();
        for (File file2 : files) {
          deleteFile(file2);
        }
      }
      file.delete();
    }
  }
  
  /**
   * Get file size(include directory)
   * @param file
   * @return
   */
  public static long getFileSize(File file) {
    long size = 0L;
    if(file.exists()) {
      size += file.length();
      if(file.isDirectory()) {
        File [] files = file.listFiles();
        for (File file2 : files) {
          size += getFileSize(file2);
        }
      }
    }
    return size;
  }
  
  /**
   * Change byte to KB/MB/GB...?keep two float point?
   * @param context
   * @param size
   * @return
   */
  public static String formatByte(Context context, long size){
    return Formatter.formatFileSize(context, size);// Change byte to KB or MB, etc.
  }
  
  /**
   * Change byte to KB/MB/GB...(Keep Integer)
   * @param context
   * @param size
   * @return
   */
  public static String formatByteFixed(long size){
    if(size <= 0) return "0B";
    if(size < 1024) return size + "B"; else size = size/1024;
    if(size < 1024) return size + "KB"; else size = size/1024;
    if(size < 1024) return size + "MB"; else size = size/1024;
    if(size < 1024) return size + "GB"; else size = size/1024;
    if(size < 1024) return size + "TB"; else size = size/1024;
    if(size < 1024) return size + "PB"; else size = size/1024;
    if(size < 1024) return size + "EB"; else size = size/1024;
    if(size < 1024) return size + "ZB"; else size = size/1024;
    if(size < 1024) return size + "YB"; else size = size/1024;
    if(size < 1024) return size + "NB"; else size = size/1024;
    if(size < 1024) return size + "DB"; else size = size/1024; return size + "CB";
  }
  
  /**
   * Parse a content uri to a file.
   * @param uri
   * @return
   */
  public static File parseUriToFile(Context context, Uri uri) {
    if(uri == null) {
      return null;
    }
    File file = null;
    String path = uri.getPath();
    file = new File(path);
    if(!file.exists()) {
      ContentResolver cr = context.getContentResolver();
      String [] pro = new String [] {MediaStore.MediaColumns.DATA, };
      Cursor cursor = cr.query(uri, pro, null, null, null);
      if(cursor != null) {
        String [] cs = cursor.getColumnNames();
        for (String string : cs) {
          System.out.println(string);
        }
        if(cursor.moveToFirst()) {
          int index = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
          path = cursor.getString(index);
          if(!StringUtil.isEmpty(path)) {
            file = new File(path);
            if(!file.exists()) {
              file = null;
            }
          }
        }
        cursor.close();
      }
    }
    return file;
  }
  
  
  /**
   * Get file MIMETYPE
   * @param context
   * @param uri
   * @return
   */
  public static String getMimeType(Context context, Uri uri) {
    if(uri == null) {
      return null;
    }
    String type = null;
    File file = null;
    String path = uri.getPath();
    file = new File(path);
    if(file.exists()) {  //uri like file:///......
      type = getMIMEType(file);
    } else {  //uri like content://......
      ContentResolver cr = context.getContentResolver();
      String [] pro = new String [] {MediaStore.MediaColumns.MIME_TYPE, };
      Cursor cursor = cr.query(uri, pro, null, null, null);
      if(cursor != null) {
        if(cursor.moveToFirst()) {
          int index = cursor.getColumnIndex(pro[0]);
          type = cursor.getString(index);
        } else {
          cursor.close();
        }
      }
    }
    return type;
  }
  
  
  private static String getMIMEType(File file) {
    String type = "";
    String name = file.getName();
    // ???????
    String end = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase(Locale.getDefault());
    if (end.equals("m4a") || end.equals("mp3") || end.equals("wav") || end.equals("wmv")) {
      type = "audio";
    } else if (end.equals("mp4") || end.equals("3gp") || end.equals("mpeg")) {
      type = "video";
    } else if (end.equals("jpg") || end.equals("png") || end.equals("jpeg")
        || end.equals("bmp") || end.equals("gif")) {
      type = "image";
    } else {
      // ??????????????????
      type = "*";
    }
    type += "/*";
    return type;
  }
}




Java Source Code List

com.example.androidtest.MainActivity.java
com.yuninfo.videoextand.BaseActivity.java
com.yuninfo.videoextand.Config.java
com.yuninfo.videoextand.ReadCardActivity.java
com.yuninfo.videoextand.SendCardActivity.java
com.yuninfo.videoextand.bean.CommonReq.java
com.yuninfo.videoextand.bean.ReadVideoResp.java
com.yuninfo.videoextand.bean.SendVideoResp.java
com.yuninfo.videoextand.bean.UploadReq.java
com.yuninfo.videoextand.bean.UploadResp.java
com.yuninfo.videoextand.player.VideoPlayerActivity.java
com.yuninfo.videoextand.recorder.RecorderActivity.java
com.yuninfo.videoextand.uploader.ProgressListener.java
com.yuninfo.videoextand.uploader.ProgressMultipartEntity.java
com.yuninfo.videoextand.uploader.ProgressOutputStream.java
com.yuninfo.videoextand.uploader.UploadHandler.java
com.yuninfo.videoextand.uploader.UploadThread.java
com.yuninfo.videoextand.utils.BeanRefUtil.java
com.yuninfo.videoextand.utils.DateUtil.java
com.yuninfo.videoextand.utils.FileUtil.java
com.yuninfo.videoextand.utils.HttpUtil.java
com.yuninfo.videoextand.utils.LogUtil.java
com.yuninfo.videoextand.utils.StringUtil.java
com.yuninfo.videoextand.widget.CustomPopupWindow.java
com.yuninfo.videoextand.widget.TitleBar.java
com.yuninfo.videoextand.widget.dialog.MenuDialog.java