Untitled Quiz
22 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the purpose of the getExternalFilesDir method?

  • To list all the files in the internal storage
  • To read data from the internal storage
  • To return a public directory for common files like photos, music, etc.
  • To return a private external directory for your app with the given name (correct)
  • What is the return type of the getExternalFilesDir and Environment.getExternalStoragePublicDirectory methods?

  • java.io.FileInputStream
  • java.io.FileDescriptor
  • java.io.File (correct)
  • java.io.FileNotFoundException
  • What is the purpose of the Environment.getExternalStoragePublicDirectory method?

  • To return a private external directory for your app with a given name
  • To list all the files in the internal storage
  • To read data from the internal storage
  • To return a public directory for common files like photos, music, etc. (correct)
  • What is the parameter of the getExternalFilesDir method?

    <p>null or a name</p> Signup and view all the answers

    What is the purpose of the PrintStream class in the example code?

    <p>To write data to the file</p> Signup and view all the answers

    What is the purpose of the listFiles method in the example code?

    <p>To list all the files in the directory</p> Signup and view all the answers

    What is the primary purpose of the internal storage in Android?

    <p>To store files that are private to each app</p> Signup and view all the answers

    What is the primary function of the java.io.File class in Android?

    <p>To represent a file or directory</p> Signup and view all the answers

    What is the purpose of the getFilesDir() method in Android?

    <p>To return internal directory for your app</p> Signup and view all the answers

    What is the purpose of the openFileInput() method in Android?

    <p>To open a file for reading</p> Signup and view all the answers

    What is the purpose of the java.io.InputStream class in Android?

    <p>To represent a flow of data bytes from a source or destination</p> Signup and view all the answers

    What is the primary difference between internal and external storage in Android?

    <p>Internal storage is private to each app, while external storage is shared</p> Signup and view all the answers

    What happens to files stored in internal storage when an app is uninstalled?

    <p>The files are wiped out</p> Signup and view all the answers

    What is the primary purpose of the java.io.OutputStream class in Android?

    <p>To represent a flow of data bytes to a source or destination</p> Signup and view all the answers

    What type of objects do some methods return to read/write files?

    <p>java.io.InputStream or OutputStream</p> Signup and view all the answers

    What is the purpose of the openRawResource method in the example?

    <p>To read a file from the res/raw directory</p> Signup and view all the answers

    What is the purpose of the MODE_PRIVATE parameter in the openFileOutput method?

    <p>To indicate the file is private to the app</p> Signup and view all the answers

    What is a characteristic of internal storage?

    <p>It is private to the app</p> Signup and view all the answers

    What is a characteristic of external storage?

    <p>It can be much larger than internal storage</p> Signup and view all the answers

    What happens to the external storage when the app is uninstalled?

    <p>It is only wiped in certain cases</p> Signup and view all the answers

    What is required to read/write to the external storage?

    <p>A special permission in the AndroidManifest.xml file</p> Signup and view all the answers

    What happens when the user installs an app that requires external storage permission?

    <p>The user is prompted to confirm the app permissions</p> Signup and view all the answers

    Study Notes

    Android Storage

    • Android can read and write files from two locations: internal and external storage.
    • Both are persistent storage, meaning data remains after power-off or reboot.

    Internal Storage

    • Built into the device, guaranteed to be present.
    • Typically smaller (~1-4 GB).
    • Can't be expanded or removed.
    • Specific and private to each app.
    • Wiped out when the app is uninstalled.

    Java File and Streams

    • java.io.File: represents a file or directory.
    • Methods: canRead, canWrite, create, delete, exists, getName, getParent, getPath, isFile, isDirectory, lastModified, length, listFiles, mkdir, mkdirs, renameTo.
    • java.io.InputStream and OutputStream: represent flows of data bytes from/to a source or destination.
    • Can come from a file, network, database, memory, etc.
    • Normally not directly used; instead, passed as a parameter to other objects.

    Using Internal Storage

    • An activity has methods to read/write files:
      • getFilesDir(): returns internal directory for the app.
      • getCacheDir(): returns a "temp" directory for scrap files.
      • getResources().openRawResource(R.raw.id): reads an input file from res/raw/.
      • openFileInput("name", mode): opens a file for reading.
      • openFileOutput("name", mode): opens a file for writing.

    External Storage

    • A card inserted into the device (e.g., MicroSD card).
    • Can be much larger than internal storage (~8-32 GB).
    • Can be removed or transferred to another device if needed.
    • May not be present, depending on the device.
    • Read/writable by other apps and users; not private to the app.
    • Not wiped when the app is uninstalled, except in certain cases.

    External Storage Permission

    • Must explicitly request permission in the app's AndroidManifest.xml file.
    • User will be prompted to confirm app permissions on install.

    Using External Storage

    • Methods to read/write external storage:
      • getExternalFilesDir("name"): returns "private" external directory for the app with the given name.
      • Environment.getExternalStoragePublicDirectory(name): returns public directory for common files like photos, music, etc.
    • Pass constants for name, such as Environment.DIRECTORY_ALARMS, DIRECTORY_DCIM, etc.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    7-storage.pdf

    More Like This

    Use Quizgecko on...
    Browser
    Browser