Detailed explanation of Android external storage and internal storage, from entry to mastery

Posted by hammerloaf on Thu, 16 Dec 2021 21:42:49 +0100

The complete code is as follows:

public class ExternalActivity extends AppCompatActivity {



    private EditText infoEdt;

    private TextView txt;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_external);



        infoEdt = findViewById(R.id.info_edt);

        txt = findViewById(R.id.textView);

    }



    public void operate(View v) {

        String path = getExternalFilesDir(null).getAbsolutePath() + "/test.txt";

        Log.e("ExternalActivityTag", path);

        switch (v.getId()) {

            case R.id.save_btn:

                File file =  getExternalFilesDir(null);

                try {

                    if (!file.exists()) {

                        file.createNewFile();

                    }

                    FileOutputStream fos = new FileOutputStream(path, true);

                    String str = infoEdt.getText().toString();

                    fos.write(str.getBytes());

                } catch (IOException e) {

                    e.printStackTrace();

                }

                break;

            case R.id.read_btn:

                try {

                    FileInputStream fis = new FileInputStream(path);

                    byte[] b = new byte[1024];

                    int len = fis.read(b);

                    String str2 = new String(b, 0, len);

                    txt.setText(str2);

                } catch (Exception e) {

                    e.printStackTrace();

                }

                break;

        }

    }

}



The specific location of the print is as follows (we can know this directory in combination with the overview. We can find it under the mapping directory sdcard):

2.5. Attention

Using getExternalFilesDir() and getExternalCacheDir() to obtain the private directory of external storage does not require any permission, but if you use environment Getexternalstoragedirectory () requires read-write permission of external storage. After Android 6.0, it is not enough to declare it only in the manifest file. It also requires runtime application, that is, dynamic permission.

3, Internal storage

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

3.1 overview

First, internal storage is not memory. In Android studio, the internal storage can be found through the Device File Explorer. The folder is called data. If you want to store files in the internal storage, the files can only be accessed by your application by default, and all files created by an application are in the same directory as the application package name. That is to say, the application is created from files stored internally and is associated with this application. When an application is uninstalled, these files in the internal storage are also deleted.

3.2. Obtain internal storage location

We can use getFileDir() and getCacheDir() to obtain the directories stored internally. They are located under data/data / package name / files(cache). We also use the examples stored externally to demonstrate. We just store the data in the internal storage. Because the effects of the examples are exactly the same, we won't demonstrate it, Look at the code directly (the specific code writing method is a little different from that of external storage):

public class InternalActivity extends AppCompatActivity {



    private EditText edt;

    private TextView txt;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_internal);



        edt = findViewById(R.id.editText);

        txt = findViewById(R.id.textView);

    }



    public void operate(View v) {

        File file = new File(getFilesDir(), "getFilesDir.txt");

        switch (v.getId()) {

            case R.id.save_btn:

                try {

                    if (!file.exists()) {

                        file.createNewFile();

                    }

                    FileOutputStream fos = new FileOutputStream(file);

                    fos.write(edt.getText().toString().getBytes());

                    fos.close();

                } catch (Exception e) {



                }

                break;

            case R.id.read_btn:

                try {

                    FileInputStream fis = new FileInputStream(file);

                    byte[] b = new byte[1024];

                    int len = fis.read(b);

                    String str2 = new String(b, 0, len);

                    txt.setText(str2);

                } catch (Exception e) {

                    e.printStackTrace();

                }

                break;

        }

    }

}



4, Comparison between external storage and internal storage

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

Private directories are compared here, as shown in the following table:

|External storage internal storage|

Topics: Java Android Database Design Pattern