Android系统如何获取系统根路径SD卡路径

&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
Android6.0以上系统获取内置SD和外置SD卡路径的方法
摘要:以前写了一篇文章也是说获取内外SD卡路径的:Android获取手机自带SDCard和外置SDCard路径,但是只适用于5.0及以下的系统。在AndroidStudio中使用,StorageVolume是在Android7.0(api24)里面才添加的类/***获取SD卡路径*@parammContext*@paramis_removableSD卡是否可移除,不可移除的是内置SD卡,可移除的是外置SD卡*@return*/publicstaticStringgetStorage
以前写了一篇文章也是说获取内外SD卡路径的:Android获取手机自带SDCard和外置SDCard路径,但是只适用于5.0及以下的系统。
在Android Studio中使用,StorageVolume是在Android7.0(api24)里面才添加的类
* 获取SD卡路径
* @param mContext
* @param is_removable SD卡是否可移除,不可移除的是内置SD卡,可移除的是外置SD卡
public static String getStoragePath(Context mContext, boolean is_removable) {
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
Class&?& storageVolumeClazz =
storageVolumeClazz = Class.forName(&android.os.storage.StorageVolume&);
Method getVolumeList = mStorageManager.getClass().getMethod(&getVolumeList&);
Method getPath = storageVolumeClazz.getMethod(&getPath&);
Method isRemovable = storageVolumeClazz.getMethod(&isRemovable&);
Method getState = storageVolumeClazz.getMethod(&getState&);
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i & i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
String state = (String) getState.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (is_removable == removable) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
参考文章:http://blog.csdn.net/u/article/details/
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
Android6.0以上系统获取内置SD和外置SD卡路径的方法相关信息,包括
的信息,所有Android6.0以上系统获取内置SD和外置SD卡路径的方法相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International如何正确获得手机的内置,外置SD卡路径?
[问题点数:40分]
本版专家分:0
结帖率 97.14%
CSDN今日推荐
本版专家分:0
结帖率 97.14%
本版专家分:160
本版专家分:200
本版专家分:200
本版专家分:90
本版专家分:90
本版专家分:200
本版专家分:90
本版专家分:96
本版专家分:0
结帖率 97.14%
本版专家分:96
本版专家分:0
结帖率 97.14%
本版专家分:96
本版专家分:0
匿名用户不能发表回复!|
CSDN今日推荐博客分类:
要转载请注明出处:http://gqdy365.iteye.com/blog/1066113,有很多转载了文章不写出处,还写的是什么小编最近做项目碰到什么问题怎么解决的的然后把文章贴下面,俨然一副他们自己的文章,不知羞耻!
一、内存(ram):
android的总内存大小信息存放在系统的/proc/meminfo文件里面,可以通过读取这个文件来获取这些信息:
public void getTotalMemory() {
String str1 = "/proc/meminfo";
String str2="";
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
Log.i(TAG, "---" + str2);
} catch (IOException e) {
运行信息如下:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemTotal:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemFree:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Buffers:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Cached:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapCached:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Active:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Inactive:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapTotal:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SwapFree:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Dirty:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Writeback:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---AnonPages:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Mapped:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Slab:
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SReclaimable:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---SUnreclaim:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---PageTables:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---NFS_Unstable:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Bounce:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---CommitLimit:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Committed_AS:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocTotal:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocUsed:
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocChunk:
第一行是总内存大小(即用户可以使用的ram的大小)!其他各项的介绍大家可以看这儿:
http://bg135.com/android-phones-to-get-the-total-memory-and-available-memory.html
获取当前剩余内存(ram)大小的方法:
public long getAvailMemory() {
ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availM
二、Rom大小
public long[] getRomMemroy() {
long[] romInfo = new long[2];
//Total rom memory
romInfo[0] = getTotalInternalMemorySize();
//Available rom memory
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
romInfo[1] = blockSize * availableB
getVersion();
return romI
public long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockS
注意类型,不然相乘之后会有溢出。可用内部存储的大小不能通过getRootDirectory();
取得,之前网上传的很多都是用getRootDirectory()取得的,我测试之后发现取得的数值不对。要根据getDataDirectory();
取得。
三、sdCard大小
public long[] getSDCardMemory() {
long[] sdCardInfo=new long[2];
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
long bSize = sf.getBlockSize();
long bCount = sf.getBlockCount();
long availBlocks = sf.getAvailableBlocks();
sdCardInfo[0] = bSize * bC//总大小
sdCardInfo[1] = bSize * availB//可用大小
return sdCardI
注意类型,不然相乘之后会有溢出。
四、电池电量
private BroadcastReceiver batteryReceiver=new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
// level加%就是当前电量了
然后在activity的oncreate()方法中注册
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
五、CPU信息
public String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2="";
String[] cpuInfo={"",""};
String[] arrayOfS
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i & arrayOfString. i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
return cpuI
/proc/cpuinfo文件中第一行是CPU的型号,第二行是CPU的频率,可以通过读文件,读取这些数据!
六、系统的版本信息:
public String[] getVersion(){
String[] version={"null","null","null","null"};
String str1 = "/proc/version";
String str2;
String[] arrayOfS
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
version[0]=arrayOfString[2];//KernelVersion
localBufferedReader.close();
} catch (IOException e) {
version[1] = Build.VERSION.RELEASE;// firmware version
version[2]=Build.MODEL;//model
version[3]=Build.DISPLAY;//system version
版本信息里面还包括型号等信息。
七、MAC地址和开机时间:
public String[] getOtherInfo(){
String[] other={"null","null"};
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo.getMacAddress()!=null){
other[0]=wifiInfo.getMacAddress();
other[0] = "Fail";
other[1] = getTimes();
private String getTimes() {
long ut = SystemClock.elapsedRealtime() / 1000;
if (ut == 0) {
int m = (int) ((ut / 60) % 60);
int h = (int) ((ut / 3600));
return h + " " + mContext.getString(R.string.info_times_hour) + m + " "
+ mContext.getString(R.string.info_times_minute);
最后贴一个格式化数据的方法:
public String formatSize(long size) {
String suffix =
float fSize=0;
if (size &= 1024) {
suffix = "KB";
fSize=size / 1024;
if (fSize &= 1024) {
suffix = "MB";
fSize /= 1024;
if (fSize &= 1024) {
suffix = "GB";
fSize /= 1024;
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
StringBuilder resultBuffer = new StringBuilder(df.format(fSize));
if (suffix != null)
resultBuffer.append(suffix);
return resultBuffer.toString();
保留两位小数。
浏览 32490
都去/proc下面的文件信息是否需要root权限?
不需要的!
浏览: 1053924 次
来自: 深圳
没有源码。
你那个Jni库是自己编译的还是有现成的呢?
一个带人脸识别的智能照相机demohttp://blog.cs ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'13:38 提问
android如何获得系统默认相册路径及此路径下所有照片路径
我在做一个云相册的项目,用的是百度云。现在要获得系统默认相册的所有图片路径,在google上搜罗很多,都是把sd卡上所有相册都罗列了出来,我是安卓新手,也不太懂怎么弄,各位大神有什么好办法。。。
按赞数排序
系统默认相册路径一般都是/DCIM/camera
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()
遍历SD卡所有的文件夹以及文件夹下所有的文件,并通过文件的后缀判断文件是否属于图片文件(图片文件的后缀一般是.jpg,.png,.bmp等等),将文件的路径存储到集合中,再通过文件路径进行展示图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐这个时代,作为程序员可能要学习小程序
。之前有写过两篇小程序的开发流程及其基本搭建。小程序的使用就简单得多,不用下载安装,省流量,省安装时间,不占用桌面。...
获取手机SD卡路径之爬坑解决方案
android 系统是开源的,于是各种产商各种瞎改android系统 ,导致不同版本的手机的SD卡的路径千奇百怪。三星,HTC…等比较特殊。有时候让我们Android程序员感到很迷茫,不得不怀疑自己的人生。为什么总是坑我们这些Android程序员?抱怨是没有用的,只有不断去才坑,不断的去总结了。在这里记录下我试验的几种方案。
方案一:通过Enviroment类获取存储设备路径
android的官方文档上说,采用Enviroment.getExternalStorageDirectory()方法可以得到android设备的外置存储(即外插SDCARD),如果android设备有外插SDCARD的话就返回外插SDCARD的根目录路径,如果android设备没有外插SDCARD的话就返回android设备的内置SDCARD的路径。这套方案很快就被否决了,因为Enviroment类的这个方法里面的路径也是写死的,只有原生的android系统才使用这套方案,被更改过的anroid体统很多设备的路径都改了。
方案二:读取system/etc/vold.fstab文件的内容来获取存储设备路径
参考文档:http://blog.csdn.net/bbmiku/article/details/7937745
内置和外置SD卡的信息存在system/etc/vold.fstab 里面,我们可以从这里获得外置SD卡的路径。经本人实验,发现很多疑问。我的机子是三星I9300,我的机子没有外插SDCARD。通过eclipse获取vold.fstab文件,打开来看,有用的内容如下:
dev_mount sdcard /storage/extSdCard auto /devices/platform/s3c-sdhci.2/mmc_host/mmc1/
dev_mount sda /storage/UsbDriveA auto /devices/platform/s5p-ehci
dev_mount sdb /storage/UsbDriveB auto /devices/platform/s5p-ehci
dev_mount sdc /storage/UsbDriveC auto /devices/platform/s5p-ehci
dev_mount sdd /storage/UsbDriveD auto /devices/platform/s5p-ehci
dev_mount sde /storage/UsbDriveE auto /devices/platform/s5p-ehci
dev_mount sdf /storage/UsbDriveF auto /devices/platform/s5p-ehci
这里可没有我的内置SDCARD的路径啊,不懂。打开手机的文件系统发现我的内置的SDCARD路径是:/storage/emulated/0。于是我到eclipse的DDMS中去看下我的手机文件系统,发现storage路径下的文件结构为:
从这个文件结构可以看出,真正有内容的应该是emulated/legacy和sdcard0才对,再从后面的连接来看,最后这两个目录都应该是指向/mnt/shell/emulated/0。接着打开/mnt/shell/emulated/0来看看,果然是我的sdcard目录
这让我很疑惑,这样的话,读取vold.fstab文件来获取sdcard目录不就得不到/mnt/shell/emulated/0目录了么。方案二失败。
方案三的原理是linux命令,在命令窗口中输入 mount 或者 cat /proc/mounts 可得到系统挂载的存储。你也可以在DOS窗口中输入 adb shell -& mount ,或者 adb shell -& cat /proc/mounts 来查看( ”-&“ 符号只是一个分割符,不要输)。好,我来DOS窗口中输入adb shell -& mount 来看下会得到什么
我借来的这部手机有外插SDCARD。可以看到最后两条应该是挂载SDCARD信息了。不过它的挂载设备是/dev/fuse, 和 /dev/block/vold/179:17
。 好吧,我晕了,等等,会不会 最后两条信息才是挂载SDCARD信息呢?我的是手机因为没有外插SDCARD,所以最后一条才是挂载SDCARD信息,有外插SDCARD的,最后两条是挂载SDCARD信息。这是规律?好吧,不是规律,我又借了部手机,mount了下,发现这个猜想纯属扯淡。
利用mount命令来获取SDCARD路径的方法,
android常见的SD卡存储位置
/storage/emulated/0/
/storage/extSdCard
/mnt/external_sd/
/mnt/sdcard2/
/mnt/sdcard/external_sd/
/mnt/sdcard-ext/
/mnt/sdcard/
/storage/sdcard0/
/mnt/extSdCard/
/mnt/extsd/
/mnt/emmc/
/mnt/extern_sd/
/mnt/ext_sd/
/mnt/ext_card/
/mnt/_ExternalSD/
/sdcard/sd/
/sdcard/external_sd/
/mnt/sdcard/sd/
/mnt/exsdcard/
/mnt/sdcard/extStorages/SdCard/
/ext_card/
/storage/extSdCard
3.0以上可以通过反射获取:
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
// 获取sdcard的路径:外置和内置
String[] paths = (String[]) sm.getClass().getMethod("getVolumePaths", null).invoke(sm, null);
可以获得所有mount的SD卡,难道我要一条一条路径去遍历?就算遍历到了,我也不知道哪条是内置存储,哪条是外置存储。而且以后哪个深井冰产商又整出一条路径出来,不就没完没了了嘛。
然而很郁闷,到底怎么弄才有一套最佳方案? 搜索了好久
目前最佳方案代码
* 获取外置SD卡路径
public static List&String& getSDCardPaths() {
List&String& sdcardPaths = new ArrayList&String&();
String cmd = "cat /proc/mounts";
Runtime run = Runtime.getRuntime();
Process p = run.exec(cmd);
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineS
while ((lineStr = inBr.readLine()) != null) {
LogUtil.i("CommonUtil:getSDCardPath", lineStr);
String[] temp = TextUtils.split(lineStr, " ");
String result = temp[1];
File file = new File(result);
if (file.isDirectory() && file.canRead() && file.canWrite()) {
LogUtil.d("directory can read can write:",
file.getAbsolutePath());
sdcardPaths.add(result);
if (p.waitFor() != 0 && p.exitValue() == 1) {
LogUtil.e("CommonUtil:getSDCardPath", "命令执行失败!");
inBr.close();
in.close();
} catch (Exception e) {
LogUtil.e("CommonUtil:getSDCardPath", e.toString());
sdcardPaths.add(Environment.getExternalStorageDirectory()
.getAbsolutePath());
optimize(sdcardPaths);
for (Iterator iterator = sdcardPaths.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
Log.e("清除过后", string);
return sdcardP
private static void optimize(List&String& sdcaredPaths) {
if (sdcaredPaths.size() == 0) {
int index = 0;
while (true) {
if (index &= sdcaredPaths.size() - 1) {
String lastItem = sdcaredPaths.get(sdcaredPaths.size() - 1);
for (int i = sdcaredPaths.size() - 2; i &= 0; i--) {
if (sdcaredPaths.get(i).contains(lastItem)) {
sdcaredPaths.remove(i);
String containsItem = sdcaredPaths.get(index);
for (int i = index + 1; i & sdcaredPaths.size(); i++) {
if (sdcaredPaths.get(i).contains(containsItem)) {
sdcaredPaths.remove(i);
private static String getSecTFPath() {
String tfPath = new String();
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
LogUtil.i("getSecTFPath--line====" + line);
if (line.contains("secure"))
if (line.contains("asec"))
if (line.contains("internal"))
if (line.contains("mydoc"))
if (line.contains("firmware"))
if (line.contains("fat")) {
LogUtil.i("getSecTFPath--fat====" + line);
String columns[] = line.split(" ");
if (columns != null && columns.length & 1) {
tfPath = columns[1];
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return tfP
写一个广播来监听sdcard是否拔插来获得外置sdcard路径,然后得到外置路径之后将其存储在数据库中
private void redMyExtraSdPathByReceiver(Activity activity) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter.addDataScheme("file");
activity.registerReceiver(new SDcaedReceiver(), intentFilter);
public class SDcaedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
intent.getData().getPath();
系统角度想到的解决办法
private void redMyExtraSdPath() {
Runtime runtime = Runtime.getRuntime();
Process proc = null;
proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String mount = new String();
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("secure")) continue;
if (line.contains("asec")) continue;
if (line.contains("fat")) {
String columns[] = line.split(" ");
if (columns != null && columns.length & 1) {
mount = mount.concat("*" + columns[1] + "\n");
} else if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns != null && columns.length & 1) {
mount = mount.concat(columns[1] + "\n");
} catch (IOException e) {
e.printStackTrace();
Android 4.4 KitKat 限制第三方应用的 SD 卡读写权限
至于为什么要限制SD 卡读写权限请参考
安卓6.0除了在manifest中声明权限,还需要在运行时动态申请存储权限。
如果你觉得此文对您有所帮助,欢迎入群 QQ交流群 :
微信公众号:终端研发部
(欢迎关注学习和交流)
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 qt获取系统路径 的文章

 

随机推荐