作者:捕鱼达人2602914975 | 来源:互联网 | 2023-05-17 10:12
外置卡状态检测操作,实际上是反射StorageManager这个类,调用StorageManager类里面的getVolumeState私有方法得到,代码实现方式如下:packagecom.
外置卡状态检测操作,实际上是反射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 arrayPath = new ArrayList();
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 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相关操作,代码如下:
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()) {
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 delete(childFiles[i]);
}
file.delete();
}
}
/**
* SD卡剩余空间
*/
public static long getSDFreeSize() {
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
long blockSize = sf.getBlockSize();
long freeBlocks = sf.getAvailableBlocks();
return (freeBlocks * blockSize) / 1024 / 1024;
}
/**
* SD卡总容量
*/
public static long getSDAllSize() {
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
long blockSize = sf.getBlockSize();
long allBlocks = sf.getBlockCount();
return (allBlocks * blockSize) / 1024 / 1024;
}
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;
}