Android development to access files in the internal storage area and store files in the external storage area

data storage

Android uses a file system similar to the disk-based file systems found on other platforms. The system gives you several options for saving app data:

App-specific storage space:

Store files used only by the application, which can be stored in a dedicated directory in the internal storage volume or in other dedicated directories in the external storage space. Use a directory in internal storage to hold sensitive information that other apps should not have access to.

shared storage:

Stores files that your app intends to share with other apps, including media, documents, and other files.

Preferences:

Store private raw data in the form of key-value pairs.

database

: Use a persistence library to store structured data in a dedicated database.

Choose internal or external storage

All Android devices have two physical storage areas: "internal" and "external". These names come from the early days of Android, when most devices provided built-in non-volatile memory (internal storage), plus a removable storage medium such as an SD card (external storage). Some devices divide the persistent storage space into internal and external partitions, so even if there is no removable storage medium, there are two storage spaces, and the API behavior of the corresponding partition is consistent whether it is removable or not. On most devices, internal storage is smaller than external storage. However, internal storage is always available on all devices, so it's more reliable for storing the data your apps depend on.

Enter the file name and file content to access the file in the internal storage area and store the file in the external storage area respectively

style layout

activity_saveinfile.xml

insert image description here

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="30dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文件名字:"
            android:layout_marginLeft="20dp"
            android:textSize="20dp"></TextView>
        <EditText
            android:id="@+id/et_filename"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="80dp"
            android:textSize="20dp"></EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="文件内容:"
            android:textSize="20dp"></TextView>
        <EditText
            android:id="@+id/et_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="80dp"
            android:textSize="20dp"></EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_saveInternal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:text="存入INTERNAL文件"
            android:textColor="#020202"
            android:textSize="15dp"
            app:backgroundTint="#CFCECE"></Button>

        <Button
            android:id="@+id/btn_saveExternal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:text="存入EXTWRNAL文件"
            android:textColor="#020202"
            android:textSize="15dp"
            app:backgroundTint="#CFCECE"></Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Internal文件内容:"
            android:layout_marginLeft="20dp"
            android:textSize="20dp"></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:id="@+id/tv_showInternalContent"
            android:textSize="20dp"></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="External文件内容:"
            android:textSize="20dp"></TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:id="@+id/tv_showExternalContent"
            android:textSize="20dp"></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <Button
            android:id="@+id/btn_getInternal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:text="从INTERNA取L文件"
            android:textSize="15dp"
            android:textColor="#020202"
            app:backgroundTint="#CFCECE"></Button>

        <Button
            android:id="@+id/btn_getExternal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:text="从EXTWRNAL取文件"
            android:textColor="#020202"
            android:textSize="15dp"
            app:backgroundTint="#CFCECE"></Button>
    </LinearLayout>
</LinearLayout>

saveinfile

File storage and retrieval

package com.example.myapplication4;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class SaveInFile extends AppCompatActivity implements View.OnClickListener {
    
    
    private EditText et_name;
    private EditText et_content;
    private TextView tv_in_content;
    private TextView tv_ex_content;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    
    

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saveinfile);
        et_name = (EditText) findViewById(R.id.et_filename);
        et_content = (EditText) findViewById(R.id.et_content);
        tv_in_content = (TextView) findViewById(R.id.tv_showInternalContent);
        tv_ex_content = (TextView) findViewById(R.id.tv_showExternalContent);
        findViewById(R.id.btn_saveInternal).setOnClickListener(this);
        findViewById(R.id.btn_saveExternal).setOnClickListener(this);
        findViewById(R.id.btn_getInternal).setOnClickListener(this);
        findViewById(R.id.btn_getExternal).setOnClickListener(this);



    }

    public boolean isExternalStorageWritable() {
    
    
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    
    
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()) {
    
    
            case R.id.btn_saveInternal:
                FileOutputStream fos;
                try {
    
    
                    fos = openFileOutput(et_name.getText().toString().trim(), Context.MODE_PRIVATE);
                    fos.write(et_content.getText().toString().trim().getBytes());
                    fos.close();
                    Toast.makeText(this, "存入内部成功", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
    
    
                    e.printStackTrace();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
                break;
            case R.id.btn_saveExternal:
                if (isExternalStorageWritable()) {
    
    
                    File file = new File(Environment.getExternalStorageDirectory(), et_name.getText().toString().trim() + ".txt");
                    fos = null;
                    try {
    
    
                        fos = new FileOutputStream(file);
                        fos.write(et_content.getText().toString().trim().getBytes());
                        Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
                    } catch (FileNotFoundException e) {
    
    
                        Toast.makeText(this, "请允许\"SavelInFile\"访问您设备上的照片、\n" +
                                "媒体内容和文件\n", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    } catch (IOException e) {
    
    
                        e.printStackTrace();
                    }
                } else {
    
    
                    Toast.makeText(this, "外部存储区不可用", Toast.LENGTH_SHORT).show();
                }
                break;


            case R.id.btn_getInternal:
                FileInputStream fis;
                try {
    
    
                    fis = openFileInput(et_name.getText().toString().trim());
                    byte[] buffer = new byte[fis.available()];
                    fis.read(buffer);
                    tv_in_content.setText(getFilesDir() + "/" + et_name.getText().toString().trim() + ".txt" + ":" + new String(buffer));
                    fis.close();
                    Toast.makeText(this, "从内部读取成功", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
    
    
                    e.printStackTrace();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }

                break;

            case R.id.btn_getExternal:
                File file = new File(Environment.getExternalStorageDirectory(), et_name.getText().toString().trim() + ".txt");
                try {
    
    
                    fis = new FileInputStream(file);
                    byte[] buffer = new byte[fis.available()];
                    fis.read(buffer);
                    tv_ex_content.setText(Environment.getExternalStorageDirectory().getPath() + "/" + et_name.getText().toString().trim() + ".txt" + ":" + new String(buffer));
                    Toast.makeText(this, "从外部读取成功", Toast.LENGTH_SHORT).show();
                    fis.close();
                } catch (FileNotFoundException e) {
    
    
                    e.printStackTrace();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }

                break;

        }

    }


}

Experimental results

Access files in the internal storage area

insert image description here

Store files in external storage

insert image description here

Guess you like

Origin blog.csdn.net/qq_46556714/article/details/126580453