【问题标题】:Android open text file to read after Intent.ACTION_GET_CONTENT在 Intent.ACTION_GET_CONTENT 之后 Android 打开要读取的文本文件
【发布时间】:2015-05-01 12:06:55
【问题描述】:

流程是:

  1. 用户需要选择要使用的文本文件和弹出的默认 Android 资源管理器。
  2. 然后我想存储包含文件名的字符串,以实际打开文件进行读取。
  3. 我想打开该文件并将他重写为应用内部存储上的新文件。
  4. 我想从应用内部存储打开新创建的文件。
  5. 奖励 1 - 如果现在是 .txt 文件但 .doc,我想在上面的重写步骤 3 中将他转换为常规 .txt 文件。
    Bonus 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?
}

【问题讨论】:

  • “那么我想存储包含文件名的字符串”——这是不切实际的。首先,there is not necessarily a file that you can read。其次,您以后无需具备阅读Uri 的内容的能力。借助 Android 4.4+ 上的存储访问框架,如果存储提供程序允许,您可以获取持久权限并对Uri 的内容进行长期读取访问。在此之前,您需要立即使用内容,而不是稍后。
  • 除此之外,Stack Overflow 是针对编程问题的。你有什么问题?
  • 1.假设稍后是同一“运行”上的其他功能。基本上我想把这个文件复制到我的内部存储中以供以后使用......
  • 2.插入什么而不是“WHAT TODO?”

标签: android file storage internal


【解决方案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 而不是 */*

【讨论】:

  • 谢谢,成功了。处理 .DOC 以将它们重写为 .txt 怎么样?你知道解决办法吗?
【解决方案2】:

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

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();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多