我正在尝试从android studio内部存储中的.txt文件中读取文本

问题描述 投票:0回答:1
TextView textid;
textid = findViewById(R.id.textid);

File file = new File("/storage/emulated/0/hey.txt");
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while((line = br.readLine())!=null){
        text.append(line);
        text.append("\n");
    }
    Log.i("text", text);
    textid.setText(text);
}
    catch (Exception e) {
        e.printStackTrace();

根据我的要求,我需要在代码本身中提及文件的路径。而且,这没有用,我也没有在日志中看到任何内容。我将如何实施?

java android file android-studio textview
1个回答
0
投票

首先在清单文件中提供存储权限

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

如果写

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

您需要这些以便获得手机的权限才能授予对该文件的访问权限。

© www.soinside.com 2019 - 2024. All rights reserved.