Android data storage job_Externale, Internal implementation ideas

Assignment 1:

Preparation: Open the virtual machine and create a new job project

Step 1: Complete the layout

  1. Analysis theme (black DayNight.DarkActionBar) and title (SaveInFile)
  2. All string resource files are written
  3. The overall layout is linear, with 5 vertical lines (1 vertical, 4 horizontal)

uploading.4e448015.gifUploading ... Re-upload canceled

Step 2: Complete Internal Storage

  1. Add click event for button and create method for it

 

  1. Get two EditText and save their contents in string

uploading.4e448015.gifUploading ... Re-upload canceled

  1. Create an object output stream FileOutputStream
  2. Write files and handle exceptions

uploading.4e448015.gifUploading ... Re-upload canceled

  1. Use Toast as a reminder
6、如何获取文件所在的绝对路径 getFilesDir()

7. Understand

uploading.4e448015.gifUploading ... Re-upload canceled

Step 2: Complete External Storage

  1. Give an application access to external storage
  2. 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;
}
  1. 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

  1. 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

uploading.4e448015.gifUnsuccessful 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

  1. 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)

  1. The rendering of the job is as follows:

uploading.4e448015.gifUnsuccessful 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.

uploading.4e448015.gifUnsuccessful transfer and re-upload cancel

 

Published 7 original articles · Likes0 · Visits 182

Guess you like

Origin blog.csdn.net/weixin_43771204/article/details/105626826