Assignment 1:
Preparation: Open the virtual machine and create a new job project
Step 1: Complete the layout
- Analysis theme (black DayNight.DarkActionBar) and title (SaveInFile)
- All string resource files are written
- The overall layout is linear, with 5 vertical lines (1 vertical, 4 horizontal)
Uploading ... Re-upload canceled
Step 2: Complete Internal Storage
- Add click event for button and create method for it
- Get two EditText and save their contents in string
Uploading ... Re-upload canceled
- Create an object output stream FileOutputStream
- Write files and handle exceptions
Uploading ... Re-upload canceled
- Use Toast as a reminder
6、如何获取文件所在的绝对路径 getFilesDir()
7. Understand
Uploading ... Re-upload canceled
Step 2: Complete External Storage
- Give an application access to external storage
- Verify that external storage is available
Because the external storage area may not be available, you need to obtain the status of the external storage area through some methods to see if it is equal to MEDIA_MOUNTED
code show as below:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
- Save file to public directory
Save the public file in the External storage area, use the getExternalStoragePublicDirectory () method to get an external storage object to the directory
- Save the file to the external directory area
First add the Onclick method to the space
First determine whether the external directory is writable,
Create a folder directory object
Unsuccessful transfer and re-upload cancel
The return value is file, so is a directory created? (Check the file object to create a folder directory)
Then get the file name and file content
Create a file based on the obtained file name
Create an output stream of the file and assign the initial value
Start writing content and handle exceptions
Close the stream after writing and give a prompt
- When reading documents
First find the directory, file name, create InputStream in the read-write stream
Then according to .available () method, judge its size, and then create a Buffer Byte [] array
Start reading the file into the buffer, and close the stream after reading
To put what you read into Content, use new String (Buffer)
Assignment two (sharing preferences to save files)
- The rendering of the job is as follows:
Unsuccessful transfer and re-upload cancel
1. Understand what SharedPreferences are
SharedPreferences is a lightweight storage tool for Android. The storage structure used is the key-value key-value pair, similar to Java's Properties class, both of which store the key-value key-value pair in the configuration file. The difference is that the file content of the Properties is in the form of Key = Value, and the storage medium of SharedPreferences is a configuration file that conforms to the XML specification. The file path to save the SharedPreferences key-value pair information is / data / data / application package name / shared_prefs / filename.xml.
Unsuccessful transfer and re-upload cancel