package com.yeshi.base.utils; import android.content.Context; import android.content.SharedPreferences; import android.os.Environment; import android.os.StatFs; import com.yeshi.base.entity.SDCardEntity; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class SDCardUtil { public static long getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } public static long getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } public static boolean externalMemoryAvailable() { return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); } public static long getAvailableExternalMemorySize() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } else { return -1; } } public static long getTotalExternalMemorySize() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } else { return -1; } } public static List getExtSDCardPath() { List lResult = new ArrayList(); try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("mount"); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { if (line.contains("extSdCard")) { String[] arr = line.split(" "); String path = arr[1]; File file = new File(path); if (file.isDirectory()) { lResult.add(path); } } } isr.close(); } catch (Exception e) { } return lResult; } public static String getSotrageSize(long size) { if (size >= 1024) { float s = ((float) size / 1024); return (new java.text.DecimalFormat("#.00").format(s)) + "GB"; } else { return size + "MB"; } } @SuppressWarnings("deprecation") private static SDCardEntity getSDCardEntity(String path) { SDCardEntity se = new SDCardEntity(); se.setPath(path); try { File ff = new File(path); if (!ff.exists()) ff.mkdirs(); StatFs stat = new StatFs(path); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); long totalBlocks = stat.getBlockCount(); se.setAvailableSize(availableBlocks * blockSize); se.setTotalSize(totalBlocks * blockSize); } catch (Exception e) { File f = new File(path); if (f.exists()) { se.setAvailableSize(f.getFreeSpace()); se.setTotalSize(f.getTotalSpace()); } } return se; } /** * 获取外部存储卡 * * @param context * @return */ public static SDCardEntity getSDCardPath(Context context) { SDCardEntity entity = new SDCardEntity(); // if (PackageUtils2.getAndroidOSVersion() > 9) { StorageList list = new StorageList(context); String[] sts = list.getVolumePaths(); if (sts == null || sts.length == 0) return null; else { if (sts[0].equalsIgnoreCase(Environment.MEDIA_MOUNTED)) { entity = getSDCardEntity(sts[0]); if (entity.getTotalSize() > 0) return entity; } else if (sts.length > 1) { entity.setPath(sts[1]); entity = getSDCardEntity(sts[1]); if (entity.getTotalSize() > 0) return entity; } else return null; } /* * } else { if (Environment.getStorageState(Environment.) * .equals(Environment.MEDIA_MOUNTED)) { // 为true的话,外置sd卡存在 } } */ return null; } public static void setDeaultStorage(Context context, int type) { SharedPreferences share = context.getSharedPreferences("storagestate", Context.MODE_PRIVATE); SharedPreferences.Editor editor = share.edit(); editor.putInt("stro", type); editor.commit(); } public static void initStorage(Context context) { SharedPreferences share = context.getSharedPreferences("storagestate", Context.MODE_PRIVATE); if (!share.getBoolean("StorageSetting", false)) return; SDCardEntity entity = getSDCardPath(context); if (entity != null) { if (entity.getAvailableSize() > getAvailableExternalMemorySize()) { setDeaultStorage(context, STORAGE_SDCARD); } } SharedPreferences.Editor editor = share.edit(); editor.putBoolean("StorageSetting", true); editor.commit(); } public static int getDeaultStorage(Context context) { SharedPreferences share = context.getSharedPreferences("storagestate", Context.MODE_PRIVATE); return share.getInt("stro", 0); } public static String getDownLoadPath(Context context) { String name = context.getPackageName(); int type = getDeaultStorage(context); if (type == STORAGE_MOBILE) { // 下载文件存放到根目录 Download文件夹下 // File f = Environment // .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); // 下载文件存放到app安装目录下 File f = context.getExternalFilesDir("木瓜缓存文件"); // File f = context.getExternalCacheDir(); if (f == null) { f = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } if (!f.exists()) f.mkdirs(); return f.getPath() + "/"; } else if (type == STORAGE_SDCARD) { SDCardEntity entity = getSDCardPath(context); if (entity == null) { File f = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); if (!f.exists()) f.mkdirs(); return f.getPath() + "/"; } String root = entity.getPath(); if (!new File(root + File.separator + name).exists()) new File(root + File.separator + name).mkdirs(); if (!new File(root + File.separator + name + File.separator + "video").exists()) new File(root + File.separator + name + File.separator + "video").mkdirs(); return root + File.separator + name + File.separator + "video"; } return ""; } public static SDCardEntity getDownLoadEntity(Context context) { String path = getDownLoadPath(context); return getSDCardEntity(path); } public final static int STORAGE_SDCARD = 1; public final static int STORAGE_MOBILE = 0; }