如何访问手机的Micro Micro SD卡? [英] How to access External Micro SD card of the phone?

查看:216
本文介绍了如何访问手机的Micro Micro SD卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何获取手机的SD卡?我知道有人会告诉我它的 getExternalStorageDirectory() Environment.getExternalStoragePublicDirectory( )



但不幸的是,它并不总是指向所有型号的外部SD卡。例如,我尝试在三星的一个模式,它工作正常,但另一个不是,LG不。而且根据文档也不一定是外部的SD卡。



这里是



*不要在这里用外部这个词混淆。 / p>

此目录可以更好地被认为是媒体/共享存储,它是一个文件系统,可以容纳相对大量的数据,并且被共享跨越所有应用程序(不执行权限)。



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



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



我如何克服这个?

解决方案

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



这些是错过的: -

 文件路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
文件myFile = new File(path,test.txt);

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

文件f = getExternalFilesDir(null);
文件文件=新文件(f,test.txt);

这些都访问内部存储/ storage / sdcard0或/ storage / emulated / 0 。在Android设备中,内部存储器的一部分用作外部存储器的原因。因此,如果您的内部存储容量大约在16Gb或更大,并且可以选择使用SD卡扩展设备,那么这是我想要访问外部SD卡的唯一方法,因为即使是内置的功能和库最后我用这个: -

  String extFilePath =/ storage / sdcard1 / Download; 
文件myFile =新文件(extFilePath,test.txt);

它的工作。所以你看到预定义的android库/函数失败的地方,我可以用简单的String来完成任务。



除此之外,如果你想检查路径对于外部存储您的设备,请尝试: -

  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 = /存储/ sdcard1 /;
Log.i(Sd Card1 Path,sd1path);}

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

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

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


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

I know that someone will tell me its getExternalStorageDirectory() or 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.

Here it is,

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

This directory can better be thought as media/shared storage. 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."*

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

How can I overcome with this?

解决方案

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:-

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.

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.

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.

这篇关于如何访问手机的Micro Micro SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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