android 6.0检查设备是否挂载SD卡,USB设备
2017-07-06 18:02
459 查看
做了一个智能电视的项目, 里面需要文件管理器 , 去下载了一些相关的 ,达不到老板的需求 ,那就没办法了,只能自己撸一个了 , 也碰到许多坑 。 关于挂载SD卡的,就是一个 ,android6.0以下和6.0的更改太大 ,以前的方法几乎获取不到相关信息 ,也看了很多大神的文章 , 自己总结一下
注释比较清晰,直接看代码吧
一些常量类
public class AppConfig {
/***
* SD卡拔广播
*/
public static final String SDCARD_IN_BROADCAST = "com.reeman.filemanager.receiver.UsbBroadCastReceiver.SDCARD_IN_BROADCAST";
public static final String SDCARD_OUT_BROADCAST = "com.reeman.filemanager.receiver.UsbBroadCastReceiver.SDCARD_OUT_BROADCAST";
public static final String BASE_APK_URL = Environment.getExternalStorageDirectory().getPath() + "/reeman";
public final static String APK_DOWN_FILEDIR = BASE_APK_URL + "/apk";
/**
* 默认内置内存卡的更目录
**/
public static final String BASE_SD_PATH = Environment.getExternalStorageDirectory().getPath();
/***
* 外置SD卡存储
* Splash界面会赋值
*/
public static String EXTERNAL_SD = "";
}
注释比较清晰,直接看代码吧
一些常量类
public class AppConfig {
/***
* SD卡拔广播
*/
public static final String SDCARD_IN_BROADCAST = "com.reeman.filemanager.receiver.UsbBroadCastReceiver.SDCARD_IN_BROADCAST";
public static final String SDCARD_OUT_BROADCAST = "com.reeman.filemanager.receiver.UsbBroadCastReceiver.SDCARD_OUT_BROADCAST";
public static final String BASE_APK_URL = Environment.getExternalStorageDirectory().getPath() + "/reeman";
public final static String APK_DOWN_FILEDIR = BASE_APK_URL + "/apk";
/**
* 默认内置内存卡的更目录
**/
public static final String BASE_SD_PATH = Environment.getExternalStorageDirectory().getPath();
/***
* 外置SD卡存储
* Splash界面会赋值
*/
public static String EXTERNAL_SD = "";
}
package com.reeman.filemanager.util; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.os.Environment; import android.os.StatFs; import android.os.storage.StorageManager; import android.text.TextUtils; import android.text.format.Formatter; import android.util.Log; import com.reeman.filemanager.config.AppConfig; import com.reeman.filemanager.entity.FileLocalEntity; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class MySDCard { private String tag = this.getClass().getName(); private Context context; public static Object[] objArray; public static String path = null;// SD卡的路径 public static String TotalSize = null;// SD卡的大小 public static String AvailableSize = null;// SD卡的大小 public static Boolean isRemovable = false;// SD卡是否可移除 String getVolumeState = null;// SD卡的安装状态 public static List<FileLocalEntity> mStorageList = new ArrayList<>(); /*** * 检索当前有没有SD卡 */ public boolean searchSd () { File[] files = getSDCardPaths(); Log.i(TAG,"=====当前的files的长度=="+files.length); if (files==null) return false; if (files.length < 2) { AppConfig.EXTERNAL_SD = ""; return true; } for (int i = 0; i < files.length 4000 ; i++) { String filePath = files[i].getAbsolutePath(); if (filePath.equals(AppConfig.BASE_SD_PATH)) { //这里表示检索到内置存储卡了 } else { AppConfig.EXTERNAL_SD = filePath; } Log.i(TAG, "文件的路径 ==== " + filePath); } return true; } public static List<FileLocalEntity> getmStorageList () { return mStorageList; } /** * 构造方法 */ public MySDCard (Context context) { this.context = context; } public static int getSdkVersion () { Log.i("==", "当前手机系统版本号=" + Build.VERSION.SDK_INT); return Build.VERSION.SDK_INT; } public File[] getSDCardPaths () { Log.i(tag, "当前手机系统版本号=" + Build.VERSION.SDK_INT); switch (Build.VERSION.SDK_INT) { case 23: case 22: case 21: case 20: return this.getSDCardPaths_20(); case 19: case 18: case 17: case 16: case 15: case 14: return this.getSDCardPaths_16(); } return null; } @TargetApi(18) public File[] getSDCardPaths_20 () { try { Log.i(tag, "getExternalStorageDirectory=" + Environment.getExternalStorageDirectory().getPath()); Class class_StorageManager = StorageManager.class; Method method_getVolumeList = class_StorageManager.getMethod("getVolumeList"); Method method_getVolumeState = class_StorageManager.getMethod("getVolumeState", String.class); StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Class class_StorageVolume = Class.forName("android.os.storage.StorageVolume"); Method method_isRemovable = class_StorageVolume.getMethod("isRemovable"); Method method_getPath = class_StorageVolume.getMethod("getPath"); Method method_getId = class_StorageVolume.getMethod("getUserLabel"); Method method_getPathFile = class_StorageVolume.getMethod("getPathFile"); objArray = (Object[]) method_getVolumeList.invoke(sm); //objArray.length==2时说明插上了sd卡 List<File> fileList = new ArrayList<File>(); mStorageList.clear(); for (Object value : objArray) { //sd卡的路径 path = (String) method_getPath.invoke(value); AvailableSize = getAvailableExternalMemorySize(context, path); TotalSize = getTotalExternalMemorySize(context, path); isRemovable = (Boolean) method_isRemovable.invoke(value); String id = (String) method_getId.invoke(value); getVolumeState = (String) method_getVolumeState.invoke(sm, path);// 获取挂载状态。 File file = (File) method_getPathFile.invoke(value); fileList.add(file); if (Environment.MEDIA_MOUNTED.equals(getVolumeState)) { boolean isSdCard = !file.getAbsolutePath().equals(AppConfig.BASE_SD_PATH); FileLocalEntity fileLocalEntity = new FileLocalEntity(isSdCard, path, AvailableSize, TotalSize, getCurrentProgress(path), id, 0); mStorageList.add(fileLocalEntity); } } File[] files = new File[fileList.size()]; fileList.toArray(files); return files; } catch (Exception e) { e.printStackTrace(); } return null; } @TargetApi(16) public File[] getSDCardPaths_16 () { try { Class class_StorageManager = StorageManager.class; Method method_getVolumeList = class_StorageManager.getMethod("getVolumeList"); Method method_getVolumeState = class_StorageManager.getMethod("getVolumeState", String.class); StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Class class_StorageVolume = Class.forName("android.os.storage.StorageVolume"); Method method_isRemovable = class_StorageVolume.getMethod("isRemovable"); Method method_getPath = class_StorageVolume.getMethod("getPath"); Method method_getId = class_StorageVolume.getMethod("getUserLabel"); Object[] objArray = (Object[]) method_getVolumeList.invoke(sm); List<File> fileList = new ArrayList<File>(); mStorageList.clear(); for (Object value : objArray) { String path = (String) method_getPath.invoke(value); String getVolumeState = (String) method_getVolumeState.invoke(sm, path);// 获取挂载状态。 String id = (String) method_getId.invoke(value); AvailableSize =getLastSpace(1,path) ; TotalSize =getLastSpace(2,path) ; Log.i(TAG,"==检索存储设备的路径==="+path+" /="+getVolumeState+" /="+TotalSize ) f73c ; fileList.add(new File((String) method_getPath.invoke(value))); if (Environment.MEDIA_MOUNTED.equals(getVolumeState)) { boolean isSdCard = !path.equals(AppConfig.BASE_SD_PATH); if (id==null||id.length()<1){ if (path.contains("external_")){ id="SD卡"; }else if (path.contains("usb_")){ if (getSpaceLong(2,path)>1024*1024*10){ id="USB存储"; path = path.substring(0,path.lastIndexOf("/")); AvailableSize = "不可" ; TotalSize = "查看"; } } } FileLocalEntity fileLocalEntity = new FileLocalEntity(isSdCard, path, AvailableSize, TotalSize, getCurrentProgress(path), id, 0); mStorageList.add(fileLocalEntity); } } File[] files = new File[fileList.size()]; fileList.toArray(files); return files; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取SDCARD剩余存储空间 带单位 * * @return */ public String getAvailableExternalMemorySize (Context context, String path1) { File path = new File(path1); if (path1 == null || !path.exists()) { return 0 + "B"; } StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return Formatter.formatFileSize(context, availableBlocks * blockSize); } /** * 获取SDCARD总的存储空间 不带单位 * * @return */ public String getTotalExternalMemorySize (Context context, String path2) { File path = new File(path2); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return Formatter.formatFileSize(context, totalBlocks * blockSize); } public static final String TAG = "MySDCard"; /*** * 获取内存占用百分比 * @param path2 * @return */ public int getCurrentProgress (String path2) { File path = new File(path2); long usableSpace = path.getUsableSpace(); long totalSpace = path.getTotalSpace(); int progress = (int) ((totalSpace - usableSpace) * 100 / totalSpace); return progress; } /*** * 获取可用空间 * type * 1:剩余空间 * 2:总共空间 * @param path2 * @return */ public static String getLastSpace (int type, String path2) { String space_desc = ""; File path = new File(path2); long usableSpace; if (type == 1) { usableSpace = path.getUsableSpace(); } else { usableSpace = path.getTotalSpace(); } space_desc = FileUtil.FormetFileSize(usableSpace); return space_desc; } public static long getSpaceLong (int type, String path2) { File path = new File(path2); long usableSpace; if (type == 1) { usableSpace = path.getUsableSpace(); } else { usableSpace = path.getTotalSpace(); } return usableSpace; } public static List<String> getMountPathList() { List<String> pathList = new ArrayList<String>(); String cmd = "cat /proc/mounts"; Runtime run = Runtime.getRuntime();//取得当前JVM的运行时环境 try { Process p = run.exec(cmd);//执行命令 BufferedInputStream inputStream = new BufferedInputStream(p.getInputStream()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bufferedReader.readLine()) != null) { // 获得命令执行后在控制台的输出信息 //输出信息内容: /data/media /storage/emulated/0 sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0 String[] temp = TextUtils.split(line, "" ); //分析内容可看出第二个空格后面是路径 String result = temp[1]; File file = new File(result); //类型为目录、可读、可写,就算是一条挂载路径 if (file.isDirectory() && file.canRead() && file.canWrite()) { pathList.add(result); } // 检查命令是否执行失败 if (p.waitFor() != 0 && p.exitValue() == 1) { } } bufferedReader.close(); inputStream.close(); } catch (Exception e) { pathList.add(Environment.getExternalStorageDirectory().getAbsolutePath()); } return pathList; } }
相关文章推荐
- Android usb client mass-storage挂载单个存储设备研究
- Android Environment 判断sd卡是否挂载 获取sd卡目录
- Android 检查设备是否存在 导航栏 NavigationBar
- Android 检查设备是否联网
- [置顶] Android开发之SDCardUtils工具类。java工具详细代码,附源代码。判断SD卡是否挂载等功能
- linux 下判断USB设备是否挂载上
- Android设备挂载的所有存储器 手机存储 ,sd卡
- Android开发之SDCardUtils工具类。java工具详细代码,附源代码。判断SD卡是否挂载等功能
- Android 检查设备是否存在 导航栏 NavigationBar
- android检查sd卡是否可写
- android 检查SD卡是否存在
- android 检查SD卡是否可用
- Android usb client mass-storage 多存储设备挂载问题解决
- Android 检查设备是否存在 导航栏 NavigationBar
- Android 检查设备是否存在 导航栏 NavigationBar
- Android判断SD卡是否已经挂载的方法
- Android工具类整理(1)检查是否有网络、检查SD卡是否存在
- Android检查设备是否联网
- Android 检查当前设备是否有摄像头
- Android判断是否挂载外置sd卡