Storage Directories Where to save files Private directories

  • Slides: 9
Download presentation
Storage Directories

Storage Directories

Where to save files? • Private directories – – • By default, only accessible

Where to save files? • Private directories – – • By default, only accessible by this app (cannot share files) Deleted when app is uninstalled Usually on internal storage Can be made public if created with the MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE options External Storage – Might be on sdcard or on nonremoval memory – Can be mounted on another machine • when attaching phone to laptop via usb, the external memory is accessible by the laptop and might not be accessible by the phone apps • So you need to check if the external directories are available before trying to use them – External. Storage. Directory • Root of the sdcard • Usually, /mnt/sdcard – External. Storage. Public. Directory • Well known directories • E. g. , a dir on the sdcard for pictures – External. Files. Dir • Sort of like private directories in that a directory is made for the app – E. g. , appname/data/files • Different types of directories for pictures, videos • Not like private directories in that the directories are not private – Any app can change the files • Like private directories in the sense that they are removed if the app is uninstalled

Try different storage options • Storage. Fun app – Add permission: write_extrernal_storage • Two

Try different storage options • Storage. Fun app – Add permission: write_extrernal_storage • Two useful functions – public int extenal. Storage. Is. Available() • • Return 1 if external storage is available for reading and writing Returns 0 if external storage is available for reading only Return -1 if external storage is unavailable Use this function before to open a file in external storage – public File get. Path. For. Storage( • • Context context, boolean use. External. Storage. Public. Directory, boolean use. External. Storage. Directory, boolean use. External. Files. Dir, boolean use. Files. Dir) Returns the path to somewhere in storage. Exactly one argument should be true. Return null if no augments are true

 • Status of external storage First, writing to external storage is only allowed

• Status of external storage First, writing to external storage is only allowed if the app has permission – Add permission write_extrernal_storage • Before external storage is used, the app must check that the storage is accessible – If the phone is attached to a computer via usb, the storage might not be accessible because the computer is controlling the storage device • public int extenal. Storage. Is. Available() { – – – //Returns 1 if external storage is available for reading and writing //Returns 0 if external storage is available for reading only //Return -1 if external storage is unavailable //Use this function before to open a file in external storage String state = Environment. get. External. Storage. State(); if (Environment. MEDIA_MOUNTED. equals(state)) { • // We can read and write the media • Log. e("Storage Status", "external storage is readble and writable"); • return 1; – } else if (Environment. MEDIA_MOUNTED_READ_ONLY. equals(state)) { • // We can only read the media • Log. e("Storage Status", "external storage is readable only"); • return 0; – } else { • • • } – } // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write Log. e("Storage Status", "external storage is not accessible"); return -1;

External Storage Public Directory • If external storage is available, we can get a

External Storage Public Directory • If external storage is available, we can get a commonly used directory – Good for saving pictures so that other apps can view them – Not secure • In get. Path. For. Storage, add – File path = null; – if (use. External. Storage. Public. Directory) { • • • – } path = Environment. get. External. Storage. Public. Directory(Environment. DIRECTORY_PICTURES); // arg must not be null, can be //DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM (for Pictures and videos from camera) path. mkdirs(); Log. e("Storage Info", "path to External. Storage. Public. Directory: "+path. get. Absolute. Path());

External Storage Directory • To write to the sdcard – Usually, /mnt/sdcard. But sometimes

External Storage Directory • To write to the sdcard – Usually, /mnt/sdcard. But sometimes /sdcard • In get. Path. For. Storage, add – if (use. External. Storage. Directory) { • • – } path = Environment. get. External. Storage. Directory(); Log. e("Storage Info", " path to External. Storage. Directory : "+path. get. Absolute. Path());

Internal application storage • By default only accessible by the applications. Security is enforced

Internal application storage • By default only accessible by the applications. Security is enforced • No need for read/write permission • Deleted when the app is uninstalled • Directory is /data/<appname>/files • Options can be used to allow other to read file • In get. Path. For. Storage, add – if (use. Files. Dir) { –} • path = get. Files. Dir(); • path. mkdirs(); • Log. e("Storage Info", " path to Files. Dir : "+path. get. Absolute. Path());

External Application Storage • Like get. File. Dir in the sense that this is

External Application Storage • Like get. File. Dir in the sense that this is a dir for this app to use • However, this dir is on the sdcard (external storage) • Therefore, these files are not visible to user media applications – e. g. , if you store a movie here, your movie player might not find it – But probably it will scan all directories and find it • And, there is no security enforcement. So other applications could overwrite • Saves to /mnt/sdcard/Android/data/<appname>/files/Pictures – Instead of Pictures, could be Movies, etc. • In get. Path. For. Storage, add – if (use. External. Files. Dir) { – } • path = context. get. External. Files. Dir(Environment. DIRECTORY_PICTURES); • // type is DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, or DIRECTORY_MOVIES. • // Or null: saves to /mnt/sdcard/Android/data/<appname>/files • Log. e("Storage Info", "path to = get. External. Files. Dir : "+path. get. Absolute. Path());

Play with Storage Area • In on. Create, add – – – – File

Play with Storage Area • In on. Create, add – – – – File path = get. Path. For. Storage(this, true, false, false); File test. File = new File(path, "test. txt"); File. Output. Stream fio; try { fio = new File. Output. Stream(test. File); fio. close(); } catch (File. Not. Found. Exception e) { – } catch (IOException e) { • • – • } File. Output. Stream fos; try { • • – – } e. print. Stack. Trace(); Test this, by changing to you android-sdk/platform-tools directory and pull the file from internal storage – • e. print. Stack. Trace(); } catch (IOException e) { • – fos = open. File. Output("test. txt", Context. MODE_WORLD_READABLE|Context. MODE_WORLD_WRITEABLE); fos. close(); } catch (File. Not. Found. Exception e) { • • e. print. Stack. Trace(); To enable unrestricted access to a file in internal storage, before writing, – – • e. print. Stack. Trace(); adb –d pull /data/edu. udel. eleg 454. Storage. Fun/files/test. txt Not that you can easily pull files from your sdcard, but only files that have been created with MODE_WORLD_READABLE option can be pulled from internal memory You can also pull files from your sdcard with ddms