Java Android从URI读取文本文件

Java Android从URI读取文本文件,java,android,file,android-intent,text,Java,Android,File,Android Intent,Text,我有一个Uri指向来自intent的文本文件,我试图读取该文件以解析其中的字符串。这是我尝试过的,但由于FileNotFoundException而失败。toString()方法似乎丢失了一个/ java.io.FileNotFoundException:content:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_B56231; 3

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

java.io.FileNotFoundException:content:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_B56231; 310A_52b6ec1c_c4d5f0d3_73F7489; a_711e4cf2/untitled%20text.txt:打开失败:eNot(无此类文件或目录)

数据的价值是:

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

data.getPath()的值为

/附件/下载/528c4088144d1515d933ca406b7bc273/附件/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似乎丢失了内容中的一个斜杠://

f:

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


文件中读取文本

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

完成

您是否尝试检查此项,我发现您的文件名中有空格。@aleksamarkoni我已用%20重新编码url,而不是“”但仍然是FileNotFoundException请您在尝试时检查SD卡是否可用。另外,请检查使用
InputStream is=getContentResolver().openInputStream(uri)
获取内容。看,这显然不是答案。输入流应该来自Uri,而不是文件/路径。特别是谷歌强调内容Uri而不是文件Uri。感谢分享,它工作得非常好
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) {

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

        }