android:关于SD卡的读取:

 

获取sd卡路径方法:
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();

}

---------------------------------------------------------------------------------------------------

这里开始是另一种写法:

// 获得sd卡的内存状态
File sdcardFileDir = Environment.getExternalStorageDirectory();
String sdcardMemory = getMemoryInfo(sdcardFileDir);
// 获得手机内部存储控件的状态
File dataFileDir = Environment.getDataDirectory();
String dataMemory = getMemoryInfo(dataFileDir);
private String getMemoryInfo(File path) {
// 获得一个磁盘状态对象
StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize(); // 获得一个扇区的大小

long totalBlocks = stat.getBlockCount(); // 获得扇区的总数

long availableBlocks = stat.getAvailableBlocks(); // 获得可用的扇区数量

// 总空间
String totalMemory = Formatter.formatFileSize(this, totalBlocks * blockSize);
// 可用空间
String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize);

return "可用空间: " + availableMemory+"/ " + totalMemory ;
}



 

<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 读取SDCard权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

不建议直接写死android sd卡的路径。

 

Android中判断SD卡是否存在,并且可以进行写操作,可以使用如下代码

if (android.os.Environment.getExternalStorageState().equals(

  android.os.Environment.MEDIA_MOUNTED))
{
}

但是这里有一个问题就是我现在没有SD卡  这边的判断却还是为真;

文章为网络拼凑;

以下为成功的代码:

 

package com.lmy.zxzq.phonemanager.until;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.text.format.Formatter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;

/**
* Created by Administrator on 2017/2/16.
*/
public class FileSizeUtil {
private static final int ERROR = -1;

/**
* SDCARD是否存
*/
public static boolean externalMemoryAvailable() {
if(android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
return true;
}else{
return false;
}
}

/**
* 获取手机内部剩余存储空间
*
* @return
*/
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;
}

/**
* 获取手机内部总的存储空间
*
* @return
*/
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;
}

/**
* 获取SDCARD剩余存储空间
*
* @return
*/
public static long getAvailableExternalMemorySize(Context context) {
if (FileSizeUtil.getStoragePath(context,true).equals(null)) {
// File path = Environment.getExternalStorageDirectory();
String sss=FileSizeUtil.getStoragePath(context,true)+"/";
StatFs stat = new StatFs(sss);
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return ERROR;
}
}

/**
* 获取SDCARD总的存储空间
*
* @return
*/
public static long getTotalExternalMemorySize(Context context) {
if (FileSizeUtil.getStoragePath(context,true).equals(null)) {
//File path = Environment.getExternalStorageDirectory();
String sss=FileSizeUtil.getStoragePath(context,true)+"/";
StatFs stat = new StatFs(sss);
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
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;
}
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 float getTotalMemoryInfo(File path,Context context) {
// 获得一个磁盘状态对象
StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize(); // 获得一个扇区的大小

long totalBlocks = stat.getBlockCount(); // 获得扇区的总数

long availableBlocks = stat.getAvailableBlocks(); // 获得可用的扇区数量

// 总空间
String totalMemory = Formatter.formatFileSize(context, totalBlocks * blockSize);

float sum=Float.parseFloat(totalMemory.replace("GB","").trim());

return sum;
}
public static float getAvailableMemoryInfo(File path,Context context) {
// 获得一个磁盘状态对象
StatFs stat = new StatFs(path.getPath());

long blockSize = stat.getBlockSize(); // 获得一个扇区的大小

long totalBlocks = stat.getBlockCount(); // 获得扇区的总数

long availableBlocks = stat.getAvailableBlocks(); // 获得可用的扇区数量
// 可用空间
String availableMemory = Formatter.formatFileSize(context, availableBlocks * blockSize);
float str=Float.parseFloat(availableMemory.replace("GB","").trim());
return str;
}

/*通过反射的方式使用在sdk中被 隐藏 的类 StroageVolume 中的方法getVolumeList(),
获取所有的存储空间(Stroage Volume),然后通过参数is_removable控制,
来获取内部存储和外部存储(内外sd卡)的路径,参数 is_removable为false时得到的是内置sd卡路径,
为true则为外置sd卡路径。*/
public static String getStoragePath(Context mContext, boolean is_removale) {

StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (is_removale == removable) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}

整理得出网上所有的代码:
结论是,网上的代码的手机内存和SD卡是指:手机内存和手机内置SD卡;
而以上我整理的代码则是 内存和外置SD卡;
在手机上运行完美:但是在模拟器上会炸;
以上的获取以GB为单位;为0是以B为单位;

获取外置SD卡内存的理论,是以最下边的方法获得SD卡名,在获取内存。

 

posted @ 2017-02-17 10:21  李小姐的猫咪生活  阅读(381)  评论(0编辑  收藏  举报