从URI读取文本文件的Android操作

8
我有一个指向文本文件的Uri,来自于一个intent,我正在尝试读取文件以解析其中的字符串。这是我尝试过的内容,但它失败了并显示FileNotFoundException。toString()方法似乎会丢失“/”。
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

而数据的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:

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


你尝试过检查这个问题吗?我看到你的文件名中有空格。参考链接:https://dev59.com/vGPVa4cB1Zd3GeqP2RIh。 - aleksamarkoni
@aleksamarkoni 我已经使用%20重新编码了URL,而不是使用空格,但仍然出现FileNotFoundException。 - Chris Byatt
请问您能否检查在尝试时SD卡是否可用。http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage - aleksamarkoni
并且请检查此链接https://dev59.com/AW3Xa4cB1Zd3GeqPgZYb - aleksamarkoni
6
使用InputStream is = getContentResolver().openInputStream(uri)获取内容。请参阅http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream%28android.net.Uri%29。 - Andrei Mărcuţ
显示剩余2条评论
2个回答

12
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) {

        }

1
感谢分享,它完美地运行。 - Dreamer
谢谢您的回答!如果我可以问一下,这个 FOR 是什么?我从未见过这种语法。 - E.Akio
@E.Akio 这是 Java :) - Ucdemir
太棒了!虽然不是我期望的,但我会去搜索它 :) - E.Akio

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

这个函数将会返回字符串,根据您的需求使用它。

使用方法如下:

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

完成


1
这显然不是答案。输入流应该来自Uri,而不是File/Path。特别是在谷歌强调内容Uri而不是文件Uri的情况下。 - rommex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接