Android File (Save in Storage)
Next, I saved file in Storage.
As another choice, we can save file into Application area.
But sometimes we want to use contents which are made by application out side of this application, image, movie, etc…
In that case, we save file into storage.
Some device have external storage for example SD card, others have internal storage(HDD).
Or, a large size file should be saved in storage. Basically storage is larger than application area.
To use storage, we need to add permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Sample
We can get storage identifier as File object.
If we can get File object, can do everything. We are Java Programmer.
public class FileActivity extends Activity { private static String TAG = "File"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); File data = getFilesDir(); // Application Log.i("FilesDir", data.getAbsolutePath()); // External Storage File root = Environment.getRootDirectory(); Log.i("Environment root", root.getAbsolutePath()); File exterlStorage = Environment.getExternalStorageDirectory(); Log.i("Environment External", exterlStorage.getAbsolutePath()); File downloadCache = Environment.getDownloadCacheDirectory(); Log.i("Environment Download Cache", downloadCache.getAbsolutePath()); File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); Log.i("DCIM", dcim.getAbsolutePath()); File alarms = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS); Log.i("ALARMS", alarms.getAbsolutePath()); File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); Log.i("DOWNLOADS", downloads.getAbsolutePath()); File movies = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); Log.i("MOVIES", movies.getAbsolutePath()); File music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); Log.i("MUSIC", music.getAbsolutePath()); File notifications = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS); Log.i("NOTIFICATIONS", notifications.getAbsolutePath()); File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); Log.i("PICTURES", pictures.getAbsolutePath()); File podcasts = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS); Log.i("PODCASTS", podcasts.getAbsolutePath()); File ringtones = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES); Log.i("ROIGTONES", ringtones.getAbsolutePath()); } }
As examples, I show the results.
com.atmarkplant.forblog is my test program package name. It matches your application package name.
・Emulator Android 4.0.3
FilesDir | /data/data/com.atmarkplant.forblog/files |
Environment root | /system |
Environment External | /mnt/sdcard |
Environment DownloadCache | /cache |
DCIM | /mnt/sdcard/DCIM |
ALARMS | /mnt/sdcard/Alarms |
DOWNLOADS | /mnt/sdcard/Download |
MOVIES | /mnt/sdcard/Movies |
NOTIFICATIONS | /mnt/sdcard/Notifications |
PICTURES | /mnt/sdcard/Notifications |
PODCASTS | /mnt/sdcard/Notifications |
NOTIFICATIONS | /mnt/sdcard/Notifications |
・Nexus7 without SD card Android 4.2.2
FilesDir | /data/data/com.atmarkplant.forblog/files |
Environment root | /system |
Environment External | /strage/emulated/0 |
Environment DownloadCache | /cache |
DCIM | /strage/emulated/0/DCIM |
ALARMS | /strage/emulated/0/Alarms |
DOWNLOADS | /strage/emulated/0/Download |
MOVIES | /strage/emulated/0/Movies |
NOTIFICATIONS | /strage/emulated/0/Notifications |
PICTURES | /strage/emulated/0/Notifications |
PODCASTS | /strage/emulated/0/Notifications |
NOTIFICATIONS | /strage/emulated/0/Notifications |
This is special. Directory is located under /strage/emulated
・Fujitsu F-10D Android 4.0.3
FilesDir | /data/data/com.atmarkplant.forblog/files |
Environment root | /system |
Environment External | /mnt/sdcard |
Environment DownloadCache | /cache |
DCIM | /mnt/sdcard/DCIM |
ALARMS | /mnt/sdcard/Alarms |
DOWNLOADS | /mnt/sdcard/Download |
MOVIES | /mnt/sdcard/Movies |
NOTIFICATIONS | /mnt/sdcard/Notifications |
PICTURES | /mnt/sdcard/Notifications |
PODCASTS | /mnt/sdcard/Notifications |
NOTIFICATIONS | /mnt/sdcard/Notifications |