Android如何检测外置卡的状态及内置SDcard状态

2024-08-31 00:18

本文主要是介绍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状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122341

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与