Android 在 Intent.ACTION_GET_CONTENT 之后打开要阅读的文本文件

Android open text file to read after Intent.ACTION_GET_CONTENT

流程是:

  1. 用户需要 select 文本文件和默认 Android 浏览器弹出。
  2. 然后我想存储包含文件名的字符串,以实际打开文件进行读取。
  3. 我想打开该文件并将其重写为应用程序内部存储上的新文件。
  4. 我想从应用内部存储打开新创建的文件。
  5. 奖励 1 - 如果它现在是 .txt 文件但 .doc,我想在上面的重写步骤 3 中将他转换为常规 .txt 文件。
    奖励 2 - 如何处理大文本文件?

代码如下:

// 1. Start with user action pressing on button to select file
addButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, PICKFILE_RESULT_CODE);          
    }
});

// 2. Come back here
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICKFILE_RESULT_CODE) {
        // Get the Uri of the selected file
        Uri uri = data.getData();
        String filePathName = "WHAT TODO ?";
        LaterFunction(filePathName);
    }
}

// 3. Later here
public void LaterFunction(String filePathName) {
    BufferedReader br;
    FileOutputStream os;
    try {
        br = new BufferedReader(new FileReader("WHAT TODO ?"));
        //WHAT TODO ? Is this creates new file with 
        //the name NewFileName on internal app storage?
        os = openFileOutput("newFileName", Context.MODE_PRIVATE);                     
        String line = null;
        while ((line = br.readLine()) != null) {
            os.write(line.getBytes());
        }
        br.close();
        os.close();
        lastFunction("newFileName");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();     
    }
}

// 4. And in the end here
public void lastFunction(String newFileName) {
    //WHAT TODO? How to read line line the file 
    //now from internal app storage?
}

第 1 步:删除 String filePathName = "WHAT TODO ?";

第 2 步:将 LaterFunction(filePathName); 更改为 LaterFunction(uri);

第 3 步:将 br = new BufferedReader(new FileReader("WHAT TODO ?")); 更改为 br = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri));

这是解决您的问题所需的最低限度。

但是,*/* 的 MIME 类型将匹配任何类型的文件,而不仅仅是文本文件。不应使用 readLine() 复制二进制文件。如果您只想要纯文本文件,请使用 text/plain 而不是 */*

对于那些努力修复该代码的人来说,这里是固定的代码

ab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, PICKFILE_RESULT_CODE);
            }
        });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICKFILE_RESULT_CODE) {
            Uri uri = data.getData();
            LaterFunction(uri);
        }
    }

public void LaterFunction(Uri uri) {
        BufferedReader br;
        FileOutputStream os;
        try {
            br = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri)));
            //WHAT TODO ? Is this creates new file with
            //the name NewFileName on internal app storage?
            os = openFileOutput("newFileName", Context.MODE_PRIVATE);
            String line = null;
            while ((line = br.readLine()) != null) {
                os.write(line.getBytes());
                Log.w("nlllllllllllll",line);
            }
            br.close();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }