【问题标题】:Android Read Text File from URIAndroid 从 URI 读取文本文件
【发布时间】:2015-09-13 04:39:03
【问题描述】:

我有一个 Uri 指向来自 intent 的文本文件,我正在尝试读取该文件以解析其中的字符串。这是我尝试过的,但使用FileNotFoundException 失败了。 toString() 方法似乎丢失了 /

java.io.FileNotFoundException: content:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20. /p>

Uri data = getIntent().getData();
String text = data.toString();
if(data != null) {

    try {
       File f = new File(text);
       FileInputStream is = new FileInputStream(f); // Fails on this line
       int size = is.available();
       byte[] buffer = new byte[size];
       is.read(buffer);
       is.close();
       text = new String(buffer);
       Log.d("attachment: ", text);
    } catch (IOException e) {
        // TODO Auto-generated catch block
       e.printStackTrace();
    }
}

数据的值为:

content://com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20text.txt

data.getPath() 的值为

/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled text.txt

我现在正试图直接从 Uri 而不是路径获取文件:

Uri data = getIntent().getData();
String text = data.toString();
//...
File f = new File(text);

但 f 似乎丢失了 content://

中的一个斜线

f:

内容:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20text.txt

【问题讨论】:

标签: java android file android-intent text


【解决方案1】:
Uri uri = data.getData();

        try {
            InputStream in = getContentResolver().openInputStream(uri);


            BufferedReader r = new BufferedReader(new InputStreamReader(in));
            StringBuilder total = new StringBuilder();
            for (String line; (line = r.readLine()) != null; ) {
                total.append(line).append('\n');
            }

            String content = total.toString();



        }catch (Exception e) {

        }

【讨论】:

  • 感谢分享,完美运行
【解决方案2】:

文件中读取文本

private String readText() {
    File f = new File(Your_File_Path);
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    return byteArrayOutputStream.toString();
}

此函数将返回 String,根据您的要求使用。

如下使用:

Log.i("Text from File", readText());

完成

【讨论】:

  • 这显然不是的答案。输入流应该来自 Uri,而不是来自 File/Path。尤其是在强调内容 Uri 而不是 Google 归档 Uri 方面。
猜你喜欢
  • 2011-11-25
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多