Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

android - How do I get the external storage's path?

I'm trying to write a file to my phone.

I used Environment.getDataDirectory() to know the internal storage's path and Environment.getExternamStorageDirectory() to know the external storage's path.

But when I use Environment.getExternalStorageDirectory() as path, the file is created in internal storage. And when I use Environment.GetDataStorage() as the path, the file is not created. (I am not sure, but I can't find it in the explorer app, at least.)

I think my phone's internal storage is perceived as external storage.(In my case, it has 32 GB amount of storage)

I want to know removable storage(e.g. micro SD card) path. What should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the official documentation for getExternalStorageDirectory()

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.

So, it can be different from built-in storage in a device.

For your case, you could use getExternalStoragePublicDirectory(java.lang.String)

This is where the user will typically place and manage their own files

The path here should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM. May not be null.

Or if you want your data to be deleted whenever the user uninstalls your app, you could use getExternalFilesDir().

As these files are internal to the applications, and not typically visible to the user as media.

Also there are some differences between getFilesDir() and getExternalFilesDir()

  1. External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on environment for information in the storage state.

  2. There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...