2019独角兽企业重金招聘Python工程师标准>>>
package com.mob.getsdandphone;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Debug;
import android.os.Debug.MemoryInfo;
import android.os.Environment;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import android.util.Log;
public class IOStorageManager {
private static final int ERROR = -1;
public final static int SDK_VERSION = android.os.Build.VERSION.SDK_INT;
/**
* SDCARD是否存
*/
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
/**
* 获取手机内部剩余存储空间
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = getBlockSize(stat);
long availableBlocks = getAvailableBlocks(stat);
return availableBlocks * blockSize;
}
/**
* 获取手机内部总的存储空间
*
* @return
*/
public static long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = getBlockSize(stat);
long totalBlocks = getBlockCount(stat);
return totalBlocks * blockSize;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static long getBlockSize(StatFs stat)
{
return SDK_VERSION>17?stat.getBlockSizeLong():stat.getBlockSize();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static long getAvailableBlocks(StatFs stat)
{
return SDK_VERSION>17?stat.getAvailableBlocksLong():stat.getAvailableBlocks();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static long getBlockCount(StatFs stat)
{
return SDK_VERSION>17?stat.getBlockCountLong():stat.getBlockCount();
}
/**
* 获取SDCARD剩余存储空间
* @return
*/
public static long getAvailableExternalMemorySize()
{
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = getBlockSize(stat);
long availableBlocks = getAvailableBlocks(stat);
return availableBlocks * blockSize;
} else {
return ERROR;
}
}
/**
* 获取SDCARD总的存储空间
*
* @return
*/
public static long getTotalExternalMemorySize()
{
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = getBlockSize(stat);
long totalBlocks = getBlockCount(stat);
return totalBlocks * blockSize;
} else {
return ERROR;
}
}
/**
* 获取系统总内存
*
* @param context 可传入应用程序上下文。
* @return 总内存大单位为B。
*/
public static long getTotalMemorySize(Context context) {
String dir = "/proc/meminfo";
try {
FileReader fr = new FileReader(dir);
BufferedReader br = new BufferedReader(fr, 2048);
String memoryLine = br.readLine();
String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
br.close();
return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取当前可用内存,返回数据以字节为单位。
*
* @param context 可传入应用程序上下文。
* @return 当前可用内存单位为B。
*/
public static long getAvailableMemory(Context context)
{
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}
/**
* 返回系统为每个app分配的内存大小
* @param context
* @return
*/
public static long getPerAppShareMaxMemory(Context context)
{
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(memoryInfo);
return am.getMemoryClass();
}
/**
* 返回当前app的内存信息
* @return
*/
public static MemoryInfo getAppMemoryInfo()
{
android.os.Debug.MemoryInfo memoryInfo = new android.os.Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);
return memoryInfo;
}
/**
* 获取多个SDCARD磁盘路径
* @param cxt
* @return 如果是 Null,表示未找到路径
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static String[] getSDCardStoragePaths(Context cxt)
{
List pathsList = new ArrayList();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD)
{
if(externalMemoryAvailable())
{
File externalFolder = Environment.getExternalStorageDirectory();
if (externalFolder != null) {
pathsList.add(externalFolder.getAbsolutePath());
}
}
} else {
StorageManager storageManager = (StorageManager) cxt.getSystemService(Context.STORAGE_SERVICE);
try {
Method method = StorageManager.class.getDeclaredMethod("getVolumePaths");
method.setAccessible(true);
Object result = method.invoke(storageManager);
if (result != null && result instanceof String[]) {
String[] pathes = (String[]) result;
StatFs statFs;
for (String path : pathes) {
if (!TextUtils.isEmpty(path) && new File(path).exists()) {
statFs = new StatFs(path);
if (getBlockCount(statFs) * getBlockSize(statFs) != 0) {
pathsList.add(path);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return pathsList.toArray(new String[pathsList.size()]);
}
/**
* 打印当前内存信息
* @param context
*/
private static void displayBriefMemory(Context context)
{
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
Log.i("Memory","系统剩余内存:"+(info.availMem >> 10)+"k");
Log.i("Memory","系统是否处于低内存运行:"+info.lowMemory);
Log.i("Memory","当系统剩余内存低于"+info.threshold+"时就看成低内存运行");
}
private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0");
private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");
/**
* 单位换算
*
* @param size 单位为B
* @param isInteger 是否返回取整的单位
* @return 转换后的单位
*/
public static String formatFileSize(long size, boolean isInteger) {
DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
String fileSizeString = "0M";
if (size < 1024 && size > 0) {
fileSizeString = df.format((double) size) + "B";
} else if (size < 1024 * 1024) {
fileSizeString = df.format((double) size / 1024) + "K";
} else if (size < 1024 * 1024 * 1024) {
fileSizeString = df.format((double) size / (1024 * 1024)) + "M";
} else {
fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G";
}
return fileSizeString;
}
public static String getDeviceId()
{
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String DEVICE_ID = tm.getDeviceId();
if(DEVICE_ID==null)
{
DEVICE_ID = android.os.Build.SERIAL;
}
if(DEVICE_ID==null)
{
DEVICE_ID = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
}
return DEVICE_ID;
}
//获取文件类型
private static String getMimeType()
{
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor("mypdf.pdf");
if(mimeType ==null)
{
FileNameMap loadTable = MimeTable.loadTable();
mimeType = loadTable.getContentTypeFor("mnt/sdcard/IMG_01.jpg");
}
System.out.println(mimeType);
return mimeType;
}
}