Android内置和外置SD卡的位置获取
public class StorageUtils {
private static String TAG="123";
// 获取主存储卡路径 内置内存卡路径
public static String getPrimaryStoragePath(Context context) {
try {
StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths");
String[] paths = (String[]) getVolumePathsMethod.invoke(sm);
// first element in paths[] is primary storage path
return paths[0];
} catch (Exception e) {
Log.e(TAG, "getPrimaryStoragePath() failed", e);
}
return null;
}
//获取可用的所有外置内存卡路径
public static List<String> getSecondaryStoragePath(Context context) {
try {
List<String> sdPaths=new ArrayList<>();
StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths");
String[] paths = (String[]) getVolumePathsMethod.invoke(sm);//获取所有sd卡路径
if(paths.length>=1){
for (int i = 1; i <paths.length ; i++) {
if(getStorageState(context,paths[i])){
sdPaths.add(paths[i]);
}
}
}
return sdPaths;
} catch (Exception e) {
Log.e(TAG, "getSecondaryStoragePath() failed", e);
}
return null;
}
// 判断存储卡的路径是否可用
public static boolean getStorageState(Context context,String path) {
try {
StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[] {String.class});
String state = (String) getVolumeStateMethod.invoke(sm, path);
if(state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)||state.equals(Environment.MEDIA_MOUNTED)){
return true;
}
return false;
} catch (Exception e) {
Log.e(TAG, "getStorageState() failed", e);
return false;
}
}
}
Android内置和外置SD卡的位置获取的更多相关文章
- android获取内置和外置SD卡路径 - z
本文将介绍Android真机环境下如何获取内置和外置SD卡路径. 测试环境:三星Note3,其他手机待测试... 所需权限(AndroidManifest.xml文件里) <uses-permi ...
- Android手机外置SD卡(TF卡)的获取方法
Android手机上的外置SD卡,起初的时候,即在Android出世的前几年,那时手机的存储是十分有限的,不像现在到处可见16G.32G和64G的存储,因而那时候的手机有的厂商允许插入外置的SD卡,此 ...
- Android开发获取多个存储空间的路径(内置SD卡以及外置SD卡)
Android开发中经常会遇到多存储空间的问题,包括内置存储路径以及外置SD卡,而且有的时候会有多张外置SD卡,此时就需要获取不同的SD卡路径,然后根据需要来写入或者读出文件. 此处给出常用的SD卡工 ...
- 关于手机的内置SD卡与外置SD卡
对于安卓2.3的系统来说,Environment.getExternalStorageDirectory()获取的目录是内置SD卡还是外置SD卡是无法保证的, 和手机厂商的修改有关,只能通过Envir ...
- android4.0 中关于内外置sd卡的获取及读写权限问题
from://http://blog.chinaunix.net/uid-26727976-id-3146895.html 在2.x的版本中,在manifest中配置的权限android.permis ...
- Android 5.1.1在外置SD卡中创建文件夹
Android 4.4之后WRITE_MEDIA_STORAGE 权限仅提供给系统应用,不再授予第三方App,WRITE_EXTERNAL_STORAGE 权限,仅仅用于授权用户写 primary e ...
- Android操作外置SD卡和U盘相关文章
Android设备与外接U盘实现数据读取操作https://blog.csdn.net/true100/article/details/77775700 usbdisklibhttps://githu ...
- Android应用正确使用扩展SD卡,特别是安卓4.4以后的版本
Android 开发时如何正确获取使用扩展存储路径 在 2.x 版本中,Android设备都是单存储,第三方App写文件,必须申请 WRITE_EXTERNAL_STORAGE 权限: 在4.0之后, ...
- Android 下载文件及写入SD卡
Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...
随机推荐
- new JSONObject()报错
如果缺少下列依赖包会报错: 1.commons-beanutils-1.7.0.jar 2.commons-collections-3.2.1.jar 3.commons-lang-2.3.jar 4 ...
- Linux下GCC和Makefile实例(从GCC的编译到Makefile的引入)
一.确认已经装好了GCC和Make的软件包 可以使用whereis命令查看: 如果whereis gcc和whereis make命令有结果,说明安装了这两个软件,可以继续往下做. 二.使用GCC ...
- FPGA前世今生(三)
上期介绍了关于FPGA的IOB单元,这期我们介绍一下FPGA内部的其他资源,这些都是学好FPGA的基础.不管前世的沧桑,还是后世的风光,我们都要把我现在的时光,打好基础,学好FPGA. 大多数FPGA ...
- netty中的UDP
UDP 提供了向多个接收者发送消息的额外传输模式: 多播——传输到一个预定义的主机组: 广播——传输到网络(或者子网)上的所有主机. 本示例应用程序将通过发送能够被同一个网络中的所有主机所接收的消息来 ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- 【linux】查看进程使用的端口和端口使用情况
netstat -a 查看所有服务端口 netstat -tln 查看当前使用的端口 ps命令查看进程的id: ps aux | grep ftp 或者 pidof Name netstat命 ...
- Mysql实用知识点总结
本文介绍MYSQL相关知识,方便日常使用查阅 目录 准备 MYSQL常用命令 语言结构 sql语句 外键 自然语言全文搜索 准备 你可以使用 Navicat Premium 12 或者 MySQL W ...
- mysql 存储过程动态执行sql语句
之前经常在程序中拼接sql语句,其实我们也可以在存储过程中拼接sql 语句,动态的执行~~ 代码如下: DROP PROCEDURE IF EXISTS SearchByDoctor;CREATE P ...
- BMP格式详解
BMP格式详解 BMP文件格式详解(BMP file format) BMP文件格式,又称为Bitmap(位图)或是DIB(Device-Independent Device,设备无关位图),是Win ...
- Codeforces Round #499 (Div. 2)(1011)
Natasha is planning an expedition to Mars for nn people. One of the important tasks is to provide fo ...