-2

I want to store file in android. With the following flow:-

if(sd card is available){
 //Store in sd card...
}else{
//If sd card is not available...
//Store in phone memory..
}

The question is basically is there any way to access sd card and internal memory paths?

I understand that

  ContextWrapper cw       =   new ContextWrapper(getApplicationContext());
  File destinationDirForChequeImageInInternalMemory       =   cw.getDir("dirName", Context.MODE_PRIVATE);

always gives an internal storage location.

Is it guaranteed that the storage location is in phone memory?

Environment.getExternalStorageDirectory()

Does the above line always give sd card path? If not what does it return? Is there any way to get the sd card location? (if Environment.getExternalStorageDirectory returns phone memory path inspite of the device having an sd card in it.)

Please throw some insight into how to do this?

Sreekanth Karumanaghat
  • 3,383
  • 6
  • 44
  • 72

1 Answers1

1

Just read the android documentation on it.

https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

getExternalStorageDirectory

Added in API level 1 File getExternalStorageDirectory () Return the primary shared/external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

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

On devices with multiple users (as described by UserManager), each user has their own isolated shared storage. Applications only have access to the shared storage for the user they're running as.

In devices with multiple shared/external storage directories, this directory represents the primary storage that the user will interact with. Access to secondary storage is available through getExternalFilesDirs(String), getExternalCacheDirs(), and getExternalMediaDirs().

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String).

Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in KITKAT, read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.

Starting in KITKAT, if your application only needs to store internal data, consider using getExternalFilesDir(String), getExternalCacheDir(), or getExternalMediaDirs(), which require no permissions to read or write.

This path may change between platform versions, so applications should only persist relative paths.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • Still the question has not been answered, If Environment.getExternalStorageDirectory returns phone memory despite the device having an sd card is there any way to get sd card location? – Sreekanth Karumanaghat Aug 08 '16 at 09:41
  • @SreekanthKarumanaghat It wont do since you changed the question. Look here for the extended answer http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0 – IAmGroot Aug 08 '16 at 13:05