Android 如何访问手机的外置Micro SD卡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11716448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:34:08  来源:igfitidea点击:

How to access External Micro SD card of the phone?

androideclipse

提问by hiren soni

Does anyone know how can I get SD card of the phone?

有谁知道我如何获得手机的SD卡?

I know that someone will tell me its getExternalStorageDirectory()or Environment.getExternalStoragePublicDirectory().

我知道有人会告诉我它的getExternalStorageDirectory()Environment.getExternalStoragePublicDirectory()

But unfortunately it doesn't always point to the external SD card in all the models. For example I tried in one model of samsung it works fine but another not, LG not. And also according to the documentation also its not always external SD card.

但不幸的是,它并不总是指向所有型号中的外部 SD 卡。例如,我试过一种型号的三星它工作正常,但另一种不行,LG 不行。而且根据文档,它并不总是外部 SD 卡。

Here it is,

这里是,

*"don't be confused by the word "external" here.

*“不要被这里的“外部”这个词混淆。

This directory can better be thought as media/sharedstorage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions).

这个目录最好被认为是媒体/共享存储。它是一个文件系统,可以保存相对大量的数据,并在所有应用程序之间共享(不强制执行权限)。

Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer."*

传统上这是一张 SD 卡,但它也可以作为设备中的内置存储实现,与受保护的内部存储不同,可以作为文件系统安装在计算机上。”*

In my application I want user to use SD card only.

在我的应用程序中,我希望用户仅使用 SD 卡。

How can I overcome with this?

我怎样才能克服这个?

回答by rajpara

Check out the answer by CommonsWare on the same topic https://stackoverflow.com/a/5695129/582571

查看 CommonsWare 对同一主题的回答https://stackoverflow.com/a/5695129/582571

He mention that we can not distinguish between mobile's inbuilt external storage and removable external storage.

他提到我们无法区分手机的内置外部存储和可移动外部存储。

But by Aleadam answer https://stackoverflow.com/a/6049446/582571we can only check that is the External Storage is removable or not with isExternalStorageRemovable() function.

但是通过 Aleadam 回答https://stackoverflow.com/a/6049446/582571,我们只能使用 isExternalStorageRemovable() 函数检查外部存储是否可移动。

I hope you will get idea.

我希望你会有想法。

回答by Manmeet Khurana

I was facing the same problem. I didn't know how to access external sd card location. I was developing an app wherein I had to access the external sd card to read and write some stuff. I tried different methods including the pre-defined android libraries. These are the methods I used:-

我面临同样的问题。我不知道如何访问外部 SD 卡位置。我正在开发一个应用程序,其中我必须访问外部 SD 卡才能读取和写入一些内容。我尝试了不同的方法,包括预定义的 android 库。这些是我使用的方法:-

These were the misses:-

这些是未命中:-

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File myFile = new File(path, "test.txt"); 

File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath()+"/Download");

File f = getExternalFilesDir(null); 
File file = new File(f,"test.txt");

These all were accessing internal storage "/storage/sdcard0" or "/storage/emulated/0". The reason being in the android device the portion of the internal storage acts as an external storage. So in case you have internal storage of around 16 Gb or more and there is an option to expand the device with sd card, then this is the only way i guess to access the external sd card because even the built-in functions and libraries of the android studio will access internal storage as external storage.

这些都在访问内部存储“/storage/sdcard0”或“/storage/emulated/0”。原因是在 android 设备中,内部存储的一部分充当外部存储。因此,如果您有大约 16 Gb 或更多的内部存储空间,并且可以选择使用 sd 卡扩展设备,那么我猜这是访问外部 sd 卡的唯一方法,因为即使是内置函数和库android studio 将访问内部存储作为外部存储。

Finally I used this:-

最后我用了这个:-

String extFilePath = "/storage/sdcard1/Download";
File myFile = new File(extFilePath, "test.txt");

and it worked. So you see where pre-defined android libraries/functions fail, I was able to do the task with the simple String.

它奏效了。所以你看到预定义的 android 库/函数失败的地方,我能够用简单的 String 完成任务。

Apart from this if you want to check the path for external storage your device, try this:-

除此之外,如果您想检查设备的外部存储路径,请尝试以下操作:-

String sdpath,sd1path,usbdiskpath,sd0path;

if(new File("/storage/extSdCard/").exists())
{sdpath="/storage/extSdCard/";
Log.i("Sd Cardext Path", sdpath);}

if(new File("/storage/sdcard1/").exists())
{sd1path="/storage/sdcard1/";
Log.i("Sd Card1 Path",sd1path);}

if(new File("/storage/usbcard1/").exists())
{usbdiskpath="/storage/usbcard1/";
Log.i("USB Path",usbdiskpath);}

if(new File("/storage/sdcard0/").exists())
{sd0path="/storage/sdcard0/";
Log.i("Sd Card0 Path",sd0path);}

Checking these might help you know what path to choose while accessing external sd card. I hope this helps others.

检查这些可能会帮助您了解在访问外部 SD 卡时要选择的路径。我希望这对其他人有帮助。

回答by Nirali

You can below condition to check whether SDCard is available or not

您可以在以下条件下检查 SDCard 是否可用

if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {

     //Check for the file
     File appFolder = new File(Environment.getExternalStorageDirectory() + File.separator
                + context.getString(R.string.app_name));

     boolean exist = appFolder.exists();
}

回答by AmmY

Removable storage path is diff in device to device.
The example of Removable path are:
1. mnt/ext
2. mnt/externalsd
3. mnt/external_sd

可移动存储路径因设备而异。
可移动路径的例子是:
1. mnt/ext
2. mnt/externalsd
3. mnt/external_sd


My device use mnt/external_sd.


我的设备使用 mnt/external_sd。

you can check the path of your file from vold.fstab file.
this file is under systems folder.
The path of file is:

您可以从 vold.fstab 文件中检查文件的路径。
该文件位于系统文件夹下。
文件路径为:

“/etc/vold.fstab”

回答by Sweta Kumari

I was facing the same problem with my latest device. Then I found that removable SD card can be accessed at /mnt/ext_sdcard/. and it worked for me. I was able to list all the files stored in removable external sd card at this location.

我的最新设备遇到了同样的问题。然后我发现可以在/mnt/ext_sdcard/访问可移动SD卡。它对我有用。我能够在此位置列出存储在可移动外部 SD 卡中的所有文件。

Following is the code: new File("/mnt/ext_sdcard/").listFiles();

以下是代码: new File("/mnt/ext_sdcard/").listFiles();

回答by Rushi

You can get the path like the following way.....

您可以通过以下方式获取路径.....

File sdCardRoot = Environment.getExternalStorageDirectory();
        PATH = sdCardRoot.toString();

If the path not exist, then you have to make the path by mkdir().....

如果路径不存在,那么你必须通过 mkdir().....

private File getTempFile(Context context) {
        final File path = new File(Environment.getExternalStorageDirectory(),
                context.getPackageName());
        if (!path.exists()) {
            path.mkdir();
        }
        return new File(path, "new.png");
    }

The above function will return the file...

上面的函数将返回文件...