Yeluzi Playing with Android (4) External storage of data storage

Table of contents

foreword

1. Write a click event layout

2. Initialize the registration control and click event

3. Obtain the external storage path and write data

4. Check whether the SD card is mounted

5. Get the remaining space of the SD card 

6. Attach the complete code at the end


foreword

        0 Basics of Android development, I learned external storage knowledge today, I want to record it so that I can review it better in the future. In order to better interpret this knowledge, the click events of the three buttons are used to store data on the SD card, check whether the SD card is mounted, and obtain the remaining space of the SD card. The realization idea is as follows

 

1. Write a click event layout

        The xml file code and effect diagram are as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_write_sd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="往sd卡写数据"/>

    <Button
        android:id="@+id/btn_check_sd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="检查sd卡是否存在"/>

    <Button
        android:id="@+id/btn_read_sd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取SD卡的可用空间"/>

</LinearLayout>

2. Initialize the registration control and click event

        Personal habits like to write control registration and click event registration outside the onCreate method. Here, the parameter of setOnClickListener uses this, because this class inherits the View.OnClickLisener method

 

3. Obtain the external storage path and write data

        The onClick function is a specific implementation function of the click event, which is jumped from this; here, the switch case method is used to determine which button is clicked, so as to perform the corresponding operation, as follows: write data operation

 

4. Check whether the SD card is mounted

5. Get the remaining space of the SD card 

 

6. Attach the complete code at the end

package com.example.qqlogindemo;

import android.os.Bundle;
import android.os.Environment;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;

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

import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class SDCardDemoActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "SDCardDemoActivity";
    private Button mWriteSD;
    private Button mCheckSD;
    private Button mReadSD;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sd_card);
        // 注册控件
        initView();
        // 注册点击事件
        initListener();
    }

    private void initView() {
        mWriteSD = findViewById(R.id.btn_write_sd);
        mCheckSD = findViewById(R.id.btn_check_sd);
        mReadSD = findViewById(R.id.btn_read_sd);
    }

    private void initListener() {
        mWriteSD.setOnClickListener(this);
        mCheckSD.setOnClickListener(this);
        mReadSD.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_write_sd:
                // 获取SD卡(外部存储)存储路径
                File exFile = Environment.getExternalStorageDirectory();
                Log.d(TAG, "onClick: sd file dir =====" + exFile);
                try {
                    // 写入数据
                    File file = new File(exFile,"info.txt");
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write("laochengdama".getBytes(StandardCharsets.UTF_8));
                    fos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case R.id.btn_check_sd:
                // 检查是否存在SD卡
                String state = Environment.getExternalStorageState();
                if (state.equals(Environment.MEDIA_MOUNTED)){
                    Log.d(TAG, "onClick: SD卡已挂载,可以使用");
                }else if (state.equals(Environment.MEDIA_REMOVED)){
                    Log.d(TAG, "onClick: SD卡已删除,无法使用");
                }
                break;
            case R.id.btn_read_sd:
                // 获取SD卡(外部存储)存储路径
                File exFile_1 = Environment.getExternalStorageDirectory();
                long freeSpace = exFile_1.getFreeSpace();
                // 将long类型转成我们直观的空间大小,例如多少M,多少KB,多少GB
                String sizeText = Formatter.formatFileSize(this, freeSpace);
                Log.d(TAG, "onClick: ferr size =====" + sizeText);
                break;
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_60030015/article/details/126577729