安卓开发之在internal存储区中存取文件和外部存储区存放文件

数据存储

Android 使用的文件系统类似于其他平台上基于磁盘的文件系统。该系统为您提供了以下几种保存应用数据的选项:

应用专属存储空间:

存储仅供应用使用的文件,可以存储到内部存储卷中的专属目录或外部存储空间中的其他专属目录。使用内部存储空间中的目录保存其他应用不应访问的敏感信息。

共享存储:

存储您的应用打算与其他应用共享的文件,包括媒体、文档和其他文件。

偏好设置:

以键值对形式存储私有原始数据。

数据库

:使用持久性库将结构化数据存储在专用数据库中。

选择内部还是外部存储区

所有的Android设备有两个物理存储区域:“internal” 和"external"。这些名字来自于Android早期,那时大部分设备提供内置的非易失内存(internal存储),再加一个可移除的存储媒介,如SD卡(external存储)。一些设备把持久的存储空间分为了intenal和external分区,所以即使没有可移除的存储媒介,也有两种存储空间,并且不管是不是可移除的,相应分区的API行为也是一致的。在大多数设备上,内部存储空间小于外部存储空间。不过,所有设备上的内部存储空间都是始终可用的,因此在存储应用所依赖的数据时更为可靠。

输入文件名和文件内容分别在internal存储区中存取文件和外部存储区存放文件

样式布局

activity_saveinfile.xml

在这里插入图片描述

<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

文件的存储和取出

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;

        }

    }


}

实验结果

internal存储区中存取文件

在这里插入图片描述

外部存储区存放文件

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_46556714/article/details/126580453