1 / 9

Storage Directories

Storage Directories. 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

chapa
Télécharger la présentation

Storage Directories

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Storage Directories

  2. 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 • ExternalStorageDirectory • Root of the sdcard • Usually, /mnt/sdcard • ExternalStoragePublicDirectory • Well known directories • E.g., a dir on the sdcard for pictures • ExternalFilesDir • 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

  3. Try different storage options • StorageFun app • Add permission: write_extrernal_storage • Two useful functions • public intextenalStorageIsAvailable() • 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 getPathForStorage( • Context context, • booleanuseExternalStoragePublicDirectory, • booleanuseExternalStorageDirectory, • booleanuseExternalFilesDir, • booleanuseFilesDir) • Returns the path to somewhere in storage. • Exactly one argument should be true. Return null if no augments are true

  4. 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 intextenalStorageIsAvailable() { • //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.getExternalStorageState(); • 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; • } • }

  5. 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 getPathForStorage, add • File path = null; • if (useExternalStoragePublicDirectory) { • path = Environment.getExternalStoragePublicDirectory(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 ExternalStoragePublicDirectory: "+path.getAbsolutePath()); • }

  6. External Storage Directory • To write to the sdcard • Usually, /mnt/sdcard. But sometimes /sdcard • In getPathForStorage, add • if (useExternalStorageDirectory) { • path = Environment. getExternalStorageDirectory(); • Log.e("Storage Info"," path to ExternalStorageDirectory : "+path.getAbsolutePath()); • }

  7. 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/data/<appname>/files • Options can be used to allow other to read file • In getPathForStorage, add • if (useFilesDir) { • path = getFilesDir(); • path.mkdirs(); • Log.e("Storage Info"," path to FilesDir : "+path.getAbsolutePath()); • }

  8. External Application Storage • Like getFileDir 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 getPathForStorage, add • if (useExternalFilesDir) { • path = context.getExternalFilesDir(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 = getExternalFilesDir : "+path.getAbsolutePath()); • }

  9. Play with Storage Area • In onCreate, add • File path = getPathForStorage(this, true, false, false, false); • File testFile = new File(path, "test.txt"); • FileOutputStreamfio; • try { • fio = new FileOutputStream(testFile); • fio.close(); • } catch (FileNotFoundException e) { • e.printStackTrace(); • } catch (IOException e) { • e.printStackTrace(); • } • To enable unrestricted access to a file in internal storage, before writing, • FileOutputStreamfos; • try { • fos = openFileOutput("test.txt", Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); • fos.close(); • } catch (FileNotFoundException e) { • e.printStackTrace(); • } catch (IOException e) { • e.printStackTrace(); • } • Test this, by changing to you android-sdk/platform-tools directory and pull the file from internal storage • adb –d pull /data/data/edu.udel.eleg454.StorageFun/files/test.txt 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

More Related