ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android:文件读写报错FileNotFoundException: /storage/emulated/0/imooc.txt: open failed: ENOENT

2020-11-27 13:01:30  阅读:518  来源: 互联网

标签:FileNotFoundException tv button storage 报错 new main id strPath


问题定位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>

在这里插入图片描述

标签:FileNotFoundException,tv,button,storage,报错,new,main,id,strPath
来源: https://blog.csdn.net/melocarter/article/details/110228000

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有