如何在android中获取内部和外部sdcard路径 [英] How to get the internal and external sdcard path in android

查看:30
本文介绍了如何在android中获取内部和外部sdcard路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数新的 android 设备都有一个内部 sdcard 和一个外部 sdcard.我想制作一个文件浏览器应用程序,但我找不到如何在我的应用程序中使用的路径,因为

File file = Environment.getExternalStorageDirectory();

在大多数设备中只返回/mnt/sdcard但是对于其他外部 sdcard 有另一条路径,例如 /storage1 或/storage2.任何帮助表示赞赏.

解决方案

android中如何获取内外部sdcard路径

在内部存储中存储的方法:

File getDir (String name, int mode)文件 getFilesDir()

上述方法存在于Context

存储在手机内存中的方法:

文件getExternalStorageDirectory()文件 getExternalFilesDir(字符串类型)文件 getExternalStoragePublicDirectory(字符串类型)

一开始大家都用Environment.getExternalStorageDirectory(),指向手机内存的root.结果,根目录被随机内容填满.

后来添加了这两个方法:

Context 类中,他们添加了getExternalFilesDir(),指向手机内存中特定于应用程序的目录.卸载应用程序时,该目录及其内容将被删除.

Environment.getExternalStoragePublicDirectory() 用于集中存储众所周知的文件类型,如照片和电影.卸载应用程序时,该目录及其内容不会被删除.

存储在可移动存储设备中的方法,即微型 SD 卡

在 API 级别 19 之前,没有正式的方式存储在 SD 卡中.但许多人可以使用非官方 API 来做到这一点.

官方在API 级别19(Android 4.4 - Kitkat)的Context 类中引入了一种方法.

File[] getExternalFilesDirs(字符串类型)

<块引用>

它返回所有应用程序特定目录的绝对路径应用程序可以放置的共享/外部存储设备它拥有的持久文件.这些文件在应用程序内部,并且通常不会作为媒体对用户可见.

这意味着,它将返回 Micro SD 卡和内部存储器的路径.一般第二个返回路径是micro SD卡的存储路径.

Google/官方 Android 文档中的内部和外部存储术语与我们的想法完全不同.

Most of the new android devices have an internal sdcard and an external sdcard. I want to make a file explorer app but I can't find out how to get the path to use in my app because

File file = Environment.getExternalStorageDirectory();

just returns in most device /mnt/sdcard but there is another path for the other external sdcard like /storage1 or /storage2 . Any help appreciated.

解决方案

How to get the internal and external sdcard path in android

Methods to store in Internal Storage:

File getDir (String name, int mode)

File getFilesDir () 

Above methods are present in Context class

Methods to store in phone's internal memory:

File getExternalStorageDirectory ()

File getExternalFilesDir (String type)

File getExternalStoragePublicDirectory (String type)

In the beginning, everyone used Environment.getExternalStorageDirectory() , which pointed to the root of phone's internal memory. As a result, root directory was filled with random content.

Later, these two methods were added:

In Context class they added getExternalFilesDir(), pointing to an app-specific directory on phone's internal memory. This directory and its contents will be deleted when the app is uninstalled.

Environment.getExternalStoragePublicDirectory() for centralized places to store well-known file types, like photos and movies. This directory and its contents will NOT be deleted when the app is uninstalled.

Methods to store in Removable Storage i.e. micro SD card

Before API level 19, there was no official way to store in SD card. But many could do it using unofficial APIs.

Officially, one method was introduced in Context class in API level 19 (Android version 4.4 - Kitkat).

File[] getExternalFilesDirs (String type)

It returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

That means, it will return paths to both Micro SD card and Internal memory. Generally, second returned path would be storage path of micro SD card.

The Internal and External Storage terminology according to Google/official Android docs is quite different from what we think.

这篇关于如何在android中获取内部和外部sdcard路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆