本文主要是介绍Android如何检测外置卡的状态及内置SDcard状态,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
外置卡状态检测操作,实际上是反射StorageManager这个类,调用StorageManager类里面的getVolumeState私有方法得到,代码实现方式如下:
package com.asir.mediaplayer;import java.util.ArrayList;import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;import android.util.Log;import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;/*** @brief This class provides a number of function that can get storage device* paths/state or mount/umount storage devices to the upper application.**/
public class EnvironmentManager {private static final String LOG_TAG = "Asir";private static final String SD_PATH_MARK = "ext_sd";private static final String USB_PATH_MARK = "udisk";private StorageManager sm;/*** Constructor* * @param ctx* Activity Context***/public EnvironmentManager(Context ctx) {sm = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);}/*** @brief get list of paths of all storage** @return return storage paths stored in a string array. eg: "mnt/udisk1",* "mnt/ext_sdcard1" ...**/public String[] getStorageAllPaths() {String paths[] = null;try {paths = (String[]) sm.getClass().getMethod("getVolumePaths").invoke(sm);} catch (Exception e) {Log.e(LOG_TAG, "Call getMethod of getVolumePaths Error");e.printStackTrace();}return paths;}/*** @brief get paths of all SD storage** @return return sd storage paths stored in a string array. eg :* "mnt/ext_sdcard1" ...**/public String[] getSdAllPaths() {ArrayList<String> arrayPath = new ArrayList<String>();String paths[] = null;int i, count;try {paths = (String[]) sm.getClass().getMethod("getVolumePaths").invoke(sm);} catch (Exception e) {Log.e(LOG_TAG, "Call getMethod of getVolumePaths Error");e.printStackTrace();}if (null == paths)return null;for (i = 0; i < paths.length; i++) {if (-1 != paths[i].indexOf(SD_PATH_MARK)) {arrayPath.add(paths[i]);}}count = arrayPath.size();
使用方式如下:
package com.asir.album;import android.content.Context;public class FileStorageState {private EnvironmentManager mEM;public FileStorageState(Context context) {mEM = new EnvironmentManager(context);}public boolean getSDState() {return queryMeidaState(AppGlobalData.PATH_SD);}public boolean getUSB1State() {return queryMeidaState(AppGlobalData.PATH_USB1);}public boolean getUSB2State() {return queryMeidaState(AppGlobalData.PATH_USB2);}public boolean getUSB3State() {return queryMeidaState(AppGlobalData.PATH_USB3);}public boolean getUSB4State() {return queryMeidaState(AppGlobalData.PATH_USB4);}public boolean getUSB5State() {return queryMeidaState(AppGlobalData.PATH_USB5);}public boolean queryMeidaState(String path) {if (mEM != null) {if (mEM.getStorageState(path).equals("mounted")) {return true;}}return false;}}
SDcard相关操作,代码如下:
// 判断SD卡是否存在public static boolean isExistSDCard() {if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {return true;}return false;}// 判断文件是否存在public static boolean isUpgradeFileExist(String zipFilePath) {boolean result = false;if (isExistSDCard()) {//"/mnt/sdcard/hcn_mcu_update.zip"File file = new File(zipFilePath);if (file != null) {if (file.exists()) {result = true;}}}return result;}//删除文件private static void delete(File file) {if (file.isFile()) {file.delete();Log.e("file", "delete" + file.getAbsolutePath());return;}if (file.isDirectory()) {File[] childFiles = file.listFiles();if (childFiles == null || childFiles.length == 0) {file.delete();return;}for (int i = 0; i < childFiles.length; i++) {delete(childFiles[i]);}file.delete();}}/*** SD卡剩余空间*/public static long getSDFreeSize() {// 取得SD卡文件路径File path = Environment.getExternalStorageDirectory();StatFs sf = new StatFs(path.getPath());// 获取单个数据块的大小(Byte)long blockSize = sf.getBlockSize();// 空闲的数据块的数量long freeBlocks = sf.getAvailableBlocks();// 返回SD卡空闲大小// return freeBlocks * blockSize; //单位Byte// return (freeBlocks * blockSize)/1024; //单位KBreturn (freeBlocks * blockSize) / 1024 / 1024; // 单位MB}/*** SD卡总容量*/public static long getSDAllSize() {// 取得SD卡文件路径File path = Environment.getExternalStorageDirectory();StatFs sf = new StatFs(path.getPath());// 获取单个数据块的大小(Byte)long blockSize = sf.getBlockSize();// 获取所有数据块数long allBlocks = sf.getBlockCount();// 返回SD卡大小// return allBlocks * blockSize; //单位Byte// return (allBlocks * blockSize)/1024; //单位KBreturn (allBlocks * blockSize) / 1024 / 1024; // 单位MB}/** 获取指定文件大小*/public static long getFileSize(File file) throws Exception {long size = 0;if (file.exists()) {FileInputStream fis = null;fis = new FileInputStream(file);size = fis.available();} else {file.createNewFile();Log.e("获取文件大小", "文件不存在!");}return size / 1024 / 1024;}
这篇关于Android如何检测外置卡的状态及内置SDcard状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!