Android storage: the usage of Internal Storage and its difference from External storage

1.Shared Preferences      2.Internal Storage      3. External Storage      4. SQLite Database     5.Network Connection

Differences between Internal Storage and External Storage:

========================================

Internal storage belongs to the application and cannot be seen by the file manager.

External storage can be seen in the file browser   / mnt .

These two concepts are relative to applications. They should be understood as logical concepts and should not be understood as physical external SD card and mobile phone or mobile device memory.

When an application stores data on external storage, the data becomes common, visible and available to all.

When it exists on internal storage, only the application itself can see and use it.

For many devices without SD card, the system will create a virtual part of storage space for public storage (mainly music, documents and other media).

usage method:

========

To create a private file in internal storage and write data to it, use the following method:

1. Call openFileOutput(String fileName, int mode) method,

If the file corresponding to fileName exists, open the file. If it does not exist, create the file with mode permission and open it. This method

Returns a FileOutputStream pointing to the file corresponding to fileName. Use this FileOutputStream to write data to the file.

2. Call the write() method of the FileOutputStream object to write data to the file.

3. Call the close() method of the FileOutputStream object to close the file write stream.

Example: after writing a file named "abc.txt" to the internal memory, the "abc.txt" file will be generated in the / data/data//files / directory of the internal memory.

To read the data of private files in internal memory, use the following methods:

1. Call openFileInputStream(String fileName) method to open the file corresponding to fileName in internal memory. If the file exists, this method

Returns a FileInputStream object that points to a fileName file.

2. Call the read() method of the FileInputStream object to read the contents of the fileName file.

3. Call the close() method of FileInputStream object to close the file reading stream.

Other relevant:

If you want to save a file that cannot be modified in the application at compile time, save the file in the / res/raw / directory.

When you open this file in the program, you can call the openRawResource(int id) method. The parameter id in it represents R.raw,

After this method is opened, it will return an InputStream, which can be used to read the file. This file cannot be written to.

If there are cache files that need to be saved, but these files do not need to be saved permanently, you can call the getCacheDir() method,

After this method is executed, an empty directory named cache / will be created in the / data / directory of the internal memory (or the cache / directory will be opened),

<Android Summary of study notes+Latest mobile architecture video+Big Android interview questions+Project practice source code handout

Browser open: qq.cn.hn/FTe Open source sharing

And returns a File object pointing to the (empty) folder. In this cache / directory, you can save cache files,

When the internal memory space of the device is insufficient, the system will automatically delete some cache files in the cache / directory. However, in order to ensure the operation efficiency of the system,

The size of the cache / directory should be controlled manually. For example, it should not be greater than 1M. When a user uninstalls an application, the cache / directory is deleted along with the.

Example: call the getCacheDir() method. After this method is executed, an empty directory named cache / will be created in the / data / directory of the internal memory.

Using a login interface demonstration, the java code is as follows

===================

InternalDataWriteActivity:

public class InternalDataWriteActivity extends AppCompatActivity {

private Button mLoginBtn;
private EditText mUsernameET, mPasswordET;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shared_preferences_write);
    initViews();
    mLoginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = mUsernameET.getText().toString();// Get user name
            String password = mPasswordET.getText().toString();// Get password
            FileOutputStream fos = null;
            try {
                fos = openFileOutput("login", MODE_PRIVATE);// Get file output stream
                fos.write((username + " " + password).getBytes());// Save user name and password
                fos.flush();// Clear cache
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();// Close file output stream
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            Intent intent = new Intent();// Create Intent object
            // Specifies to jump to SharedPreferencesReadActivity
            intent.setClass(InternalDataWriteActivity.this, InternalDataReadActivity.class);
            startActivity(intent);// Realize jump
        }
    });
}
private void initViews() {
    mLoginBtn = (Button) findViewById(R.id.btn_login);
    mUsernameET = (EditText) findViewById(R.id.et_username);
    mPasswordET = (EditText) findViewById(R.id.et_password);
}

}

Tags: Android Design Pattern Interview Programmer

Posted on Sat, 20 Nov 2021 01:46:12 -0500 by red-x