How to access the external Micro SD card of the phone?

Does anyone know how I can get a phone sd card?

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

But, unfortunately, it does not always indicate an external SD card in all models. For example, I tried samsung in one model, it works fine, but the other does not, LG does not. And also, according to the documentation, also not always an external SD card.

There he is,

* "do not confuse the word" external "here.

This directory is best understood as a multimedia / shared repository . This is a file system that can store a relatively large amount of data and is shared in all applications (does not provide permissions).

Traditionally, this is an SD card, but it can also be implemented as built-in storage on a device that is different from secure internal storage and can be mounted as a file system on a computer. "*

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

How can I overcome this?

+5
source share
5 answers

I had the same problem. I did not know how to access an external SD card. I developed an application in which I had to access an external SD card to read and write some things. I tried various methods, including predefined Android libraries. These are the methods I used: -

These were 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");

"/storage/sdcard0" "/storage/emulated/0". , Android . , 16 , SD-, SD-, Android .

, : -

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

. , , Android, .

, , : -

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);}

, SD-. , .

+3

CommonsWare fooobar.com/questions/36365/...

, .

Aleadam fooobar.com/questions/142516/..., , isExternalStorageRemovable().

, .

+1

, 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();
}
+1

- .

Removable path:
1. mnt/ext
2. mnt/externalsd
3. mnt/external_sd


mnt/external_sd.

vold.fstab.

.

:

"/etc/vold.fstab"
+1

:

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

, 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");
    }

...

-2

All Articles