Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the external storage's path?

Tags:

android

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?

like image 997
soonoo Avatar asked Feb 10 '15 08:02

soonoo


People also ask

What is the path to external storage in Android?

Android External Storage Example Code getExternalFilesDir(): It returns the path to files folder inside Android/data/data/application_package/ on the SD card. It is used to store any required files for your app (like images downloaded from web or cache files).


1 Answers

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.

like image 169
Shubhang Malviya Avatar answered Sep 25 '22 21:09

Shubhang Malviya