Podcast
Questions and Answers
What is the purpose of the getExternalFilesDir method?
What is the purpose of the getExternalFilesDir method?
What is the return type of the getExternalFilesDir and Environment.getExternalStoragePublicDirectory methods?
What is the return type of the getExternalFilesDir and Environment.getExternalStoragePublicDirectory methods?
What is the purpose of the Environment.getExternalStoragePublicDirectory method?
What is the purpose of the Environment.getExternalStoragePublicDirectory method?
What is the parameter of the getExternalFilesDir method?
What is the parameter of the getExternalFilesDir method?
Signup and view all the answers
What is the purpose of the PrintStream class in the example code?
What is the purpose of the PrintStream class in the example code?
Signup and view all the answers
What is the purpose of the listFiles method in the example code?
What is the purpose of the listFiles method in the example code?
Signup and view all the answers
What is the primary purpose of the internal storage in Android?
What is the primary purpose of the internal storage in Android?
Signup and view all the answers
What is the primary function of the java.io.File class in Android?
What is the primary function of the java.io.File class in Android?
Signup and view all the answers
What is the purpose of the getFilesDir() method in Android?
What is the purpose of the getFilesDir() method in Android?
Signup and view all the answers
What is the purpose of the openFileInput() method in Android?
What is the purpose of the openFileInput() method in Android?
Signup and view all the answers
What is the purpose of the java.io.InputStream class in Android?
What is the purpose of the java.io.InputStream class in Android?
Signup and view all the answers
What is the primary difference between internal and external storage in Android?
What is the primary difference between internal and external storage in Android?
Signup and view all the answers
What happens to files stored in internal storage when an app is uninstalled?
What happens to files stored in internal storage when an app is uninstalled?
Signup and view all the answers
What is the primary purpose of the java.io.OutputStream class in Android?
What is the primary purpose of the java.io.OutputStream class in Android?
Signup and view all the answers
What type of objects do some methods return to read/write files?
What type of objects do some methods return to read/write files?
Signup and view all the answers
What is the purpose of the openRawResource method in the example?
What is the purpose of the openRawResource method in the example?
Signup and view all the answers
What is the purpose of the MODE_PRIVATE parameter in the openFileOutput method?
What is the purpose of the MODE_PRIVATE parameter in the openFileOutput method?
Signup and view all the answers
What is a characteristic of internal storage?
What is a characteristic of internal storage?
Signup and view all the answers
What is a characteristic of external storage?
What is a characteristic of external storage?
Signup and view all the answers
What happens to the external storage when the app is uninstalled?
What happens to the external storage when the app is uninstalled?
Signup and view all the answers
What is required to read/write to the external storage?
What is required to read/write to the external storage?
Signup and view all the answers
What happens when the user installs an app that requires external storage permission?
What happens when the user installs an app that requires external storage permission?
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.