package com.ysh.wpc.appupdate.util; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import android.os.StatFs; import android.util.Log; import com.ysh.wpc.appupdate.entity.SDCardEntity; 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"; } } @SuppressLint("NewApi") @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)) { // 涓簍rue鐨勮瘽锛屽缃畇d鍗″瓨鍦�} } */ 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) { File downloadStorage = getDownloadStorage(context); File downloadPath = null; // Log.i("help", "鎵嬫満鍐呭瓨"); if (downloadStorage.equals(Environment.getDataDirectory())) { downloadPath = new File(context.getFilesDir(), "video"); } else { List downloadPaths = Arrays.asList(Environment .getExternalStorageList(context)); // File[] downloadPaths = // ContextCompat.getExternalFilesDirs(context, "video"); for (int i = 0; i < downloadPaths.size(); i++) { File item = downloadPaths.get(i); // Log.i("help", item.getAbsolutePath()+"88888"); if (item.getAbsolutePath().startsWith( downloadStorage.getAbsolutePath())) { downloadPath = item; break; } } } if (!downloadPath.exists()) { if (downloadPath.mkdirs()) { Log.e("SDCardUtil", "Unable to create external download directory"); } } return downloadPath.getAbsolutePath(); } public static File getDownloadStorage(Context context) { SharedPreferences settings = context.getSharedPreferences("settings", Context.MODE_PRIVATE); String downloadRootPath = settings.getString("download_storage", ""); File downloadStorage; List externalStorages = Arrays.asList(Environment .getExternalStorageList(context)); File internalStorage = Environment.getDataDirectory(); if (externalStorages.isEmpty()) { downloadStorage = internalStorage; } else { downloadStorage = new File(downloadRootPath); if (!downloadStorage.equals(internalStorage)) { if (!externalStorages.contains(downloadStorage)) { downloadStorage = externalStorages.get(0); } } } return downloadStorage; } // 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); // // // 涓嬭浇鏂囦欢瀛樻斁鍒癮pp瀹夎鐩綍涓� // File f = context.getExternalFilesDir("鏈ㄧ摐缂撳瓨鏂囦欢"); // // File f = context.getExternalCacheDir(); // 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; }