简体   繁体   English

选择文本文件并阅读Android

[英]Choose text file and Read Android

I chose a text file from storage and got its path ( FilePath ), am trying to read the content of that text file and put it in edittext..i am using the code below to get text file data and put it in edittext (eTPronounce) 我从存储中选择了一个文本文件并得到了它的路径( FilePath ),我试图读取该文本文件的内容并将其放在edittext中。我使用下面的代码获取文本文件数据并将其放入edittext(eTPronounce) )

 File sdcard = Environment.getExternalStorageDirectory();


//Get the text filea
                File file = new File(sdcard,FilePath);

//Read text from file
                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');
                    }
                    br.close();
                }
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }

//Find the view by its i

//Set the text
           eTPronounce.setText(text);

            }
        });

If i replace FilePath (in the second line) with any directory where there is text file it works.For example if I replace FilePath with "Download/text.txt" it works . 如果我将FilePath(在第二行)替换为任何有文本文件的目录,它就可以工作。例如,如果我用“Download / text.txt”替换FilePath,它就可以工作。 I used this link to get FilePath 我使用链接获取FilePath

THANKS 谢谢

I think you should be using below constructor 我认为你应该使用下面的构造函数

File(File dir, String name)

or you can use 或者你可以使用

File(String path)

If you are specifying directory name then you only need to give the file name as shown in the first example.Otherwise you can use the second one with the complete file path 如果要指定目录名,则只需要提供文件名,如第一个示例所示。否则,您可以使用第二个文件路径和完整文件路径

if(resultCode==RESULT_OK){
    if(data == null || data.getData == null){
       //Log.e()
      return;
    }
FilePath = getPath(data.getData(),mActivity);
setfilename.setText(FilePath);
}

    public static String getPath(Uri uri,Context ctx) {
        String res = null;
        if(null==uri){
            return res;
        }
        if (uri != null && uri.toString().startsWith("file://")) {
            return uri.toString().substring("file://".length());
        }
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(uri, proj, null, null, null);
        if(cursor!=null){
            if(cursor.moveToFirst()){
                try {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    res = cursor.getString(column_index);
                }catch (Exception ignored){
                }finally {
                    closeCursor(cursor);
                }
            }
        }
        closeCursor(cursor);
        return res;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM