package com.yeshi.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.yeshi.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)) { // 为true的话,} }
|
*/
|
|
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<File> 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<File> 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);
|
//
|
// // 下载文件存放到app安裑目录
|
// 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;
|
}
|