Android-SDCard外部存储文件读写

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

一想到Android中到SdCard就要想到 Environment,写入到路径一定要用Environment,读取到路径一定要要用Environment,因为每部手机到SdCard到路径都会不同Environment.getExternalStorageDirectory()

千万不要这样写路径,例如:/mnt/sdcard/ /sdcard/storage/    等等

SdCard读文件读写操作:

package liudeli.datastorage;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class SdcardActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdcard);

        initViewListener();
    }

    private void initViewListener() {
        Button btCheckMountState = findViewById(R.id.bt_check_mount_state);
        Button btOutput = findViewById(R.id.bt_output_sdcard);
        Button btInput = findViewById(R.id.bt_input_sdcard);

        btCheckMountState.setOnClickListener(this);
        btOutput.setOnClickListener(this);
        btInput.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_check_mount_state:
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    alterToast("Sdcard可用");
                } else {
                    alterToast("Sdcard不可用");
                }
                break;
            case R.id.bt_input_sdcard: {
                File file = new File(Environment.getExternalStorageDirectory(), "MySdcard.txt");
                if (!file.exists()) {
                    alterToast("Sdcard的文件不存在");
                    return;
                }
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String result = br.readLine();
                    alterToast(result);
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case R.id.bt_output_sdcard: {
                File file = new File(Environment.getExternalStorageDirectory(), "MySdcard.txt");
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    FileWriter fileWriter = new FileWriter(file);
                    fileWriter.write("饭撒发生的噶地方舒服舒服撒冯绍峰撒");
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
            default:
                break;
        }
    }

    private void alterToast(String text) {
        Toast.makeText(SdcardActivity.this, text, Toast.LENGTH_LONG).show();
    }
}

Layout Code:

<?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/bt_check_mount_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="检查SDCard挂载状态"/>

    <Button
        android:id="@+id/bt_output_sdcard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入到Sdcard文件"
        />

    <Button
        android:id="@+id/bt_input_sdcard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取Sdcard文件"
        />

</LinearLayout>

猜你喜欢

转载自www.cnblogs.com/android-deli/p/10088515.html