问题定位1:
可能没有打开访问权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
问题定位2:
当用户从库中选取文件时,无法保证选取的文件是由其他应用添加或编辑的。因此,如果用户选择的文件,让我们说属于另一个应用程序,我们将遇到权限问题。一个快速的解决方法是将此代码添加到 Android
<manifest ... >
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
问题定位3:getExternalStorageDirectory() 废弃导致的问题
全部用getExternalFilesDir()方法进行代替
public class MainActivity extends AppCompatActivity {
EditText ed;
Button saveBtn, readBtn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = findViewById(R.id.main_ed);
saveBtn = findViewById(R.id.main_save_button);
readBtn = findViewById(R.id.main_read_button);
tv = findViewById(R.id.main_tv);
}
public void onOpearate(View v) {
String strPath = getExternalFilesDir(null) + "/imooc.txt";
Log.d("TAG", strPath);
switch (v.getId()) {
case R.id.main_save_button:
File f = new File(strPath);
try {
if (!f.exists()) {
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream(strPath, true);
String str = ed.getText().toString();
fos.write(str.getBytes());
} catch (IOException e) {
e.getStackTrace();
}
break;
case R.id.main_read_button:
try {
FileInputStream fis = new FileInputStream(strPath);
byte[] bytes = new byte[2048];
int len = fis.read(bytes);
Log.d("TAG", len+"");
String s = new String(bytes, 0, len);
tv.setText(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/main_ed"
android:layout_width="match_parent"
android:layout_height="300dp"
android:backgroundTint="#fb7299" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="@+id/main_save_button"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:background="#fb7299"
android:onClick="onOpearate"
android:text="保存"
android:textColor="#fff" />
<Button
android:id="@+id/main_read_button"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#fb7299"
android:onClick="onOpearate"
android:text="读取"
android:textColor="#fff" />
</LinearLayout>
<TextView
android:id="@+id/main_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>