如何从Android中的.txt文件获取字符串 [英] How to get String from a .txt file in android

查看:240
本文介绍了如何从Android中的.txt文件获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,该代码仅为.txt文件创建一个文件选择器,然后以字符串形式表示其内容.问题在于选择文件后没有任何反应(日志显示结果代码为-1,请求代码为0).谁能帮我?这是我选择的.txt文件:

I'm writing a code which creates a file chooser for only .txt files and then represents its contents in a String. The problem is that after selecting a file nothing happens (log says that result code is -1, and request code is 0). Can anyone help me? Here is my chooser for .txt files:

 public void onUploadClicked(View view) {
    Intent intent = new Intent();
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/plain");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, getText(R.string.select_file)), REQUEST_CODE);

}

这是我的OnActivityResult方法:

Here is my OnActivityResult method:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        if (data == null) {
            return;
        } else {
            Uri uri = data.getData();
            uploadedFile = new File(uri.getPath());
            try {
                readFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        return;
    }
}

这是我将.txt文件读入字符串的方法:

And here is my method for reading .txt file into a String:

private void readFile() throws IOException {

    uploadedString = new StringBuilder();
    BufferedReader reader = new BufferedReader(new FileReader(uploadedFile));
    String line;
    while ((line = reader.readLine()) != null) {

        uploadedString.append(line);
        uploadedString.append('\n');
    }
    Log.i("Uploaded successfully: ", uploadedString.toString());
    reader.close();
}

推荐答案

尝试一下

public static int PICK_FILE = 1;

然后覆盖onActivityResult()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_FILE) {
            if (resultCode == RESULT_OK) {
                // User pick the file
                Uri uri = data.getData();
                String fileContent = readTextFile(uri);
                Toast.makeText(this, fileContent, Toast.LENGTH_LONG).show();
            } else {
                Log.i(TAG, data.toString());
            }
        }
    }

读取用户选择的文本文件的方法

Method to read the text file picked by user

    private String readTextFile(Uri uri){
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri)));
            String line = "";

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return builder.toString();
    }

创建隐式意图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
startActivityForResult(intent, PICK_FILE);

这篇关于如何从Android中的.txt文件获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆