<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Android系统私有的外部存储,是随着应用程序的卸载而卸载,公有的外部存储则不会
package com.example.testapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ExternalActivity extends ApplicationActivity{
EditText infoEdt;
TextView txt;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_external);
infoEdt = findViewById(R.id.info_edt);
txt = findViewById(R.id.textView6);
// 外部存储的动态权限
int permisson = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permisson!= PackageManager.PERMISSION_GRANTED){
// 动态去申请权限
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
}
public void operate(View v){
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/imooc.txt";
Log.e("TAG",path);
switch (v.getId()){
// 保存
case R.id.save_btn:
File f = new File(path);
try{
if(!f.exists()){
f.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 (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
<?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">
<EditText
android:id="@+id/info_edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxLines="12"
android:minLines="12"
android:hint="请输入待存储的内容..."
android:gravity="left|top"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/read_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="读取"
android:onClick="operate" />
<Button
android:id="@+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="保存"
android:onClick="operate" />
</LinearLayout>
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="176dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
/>
</LinearLayout>