Chris Byatt
Chris Byatt

Reputation: 3829

Android Read Text File from URI

I've got a Uri pointing to a text file from an intent and I'm trying to read the file to parse the string inside it. This is what I've tried but it's failing with FileNotFoundException. The toString() method appears to lose a /

java.io.FileNotFoundException: content:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20text.txt: open failed: ENOENT (No such file or directory)

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

The value of data is:

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

and the value of data.getPath() is

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

I'm now trying to get the file directly from the Uri rather than the path:

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

But f appears to lose one of the slashes from content://

f:

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

Upvotes: 8

Views: 11075

Answers (2)

Ucdemir
Ucdemir

Reputation: 3098

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

        }

Upvotes: 13

Hiren Patel
Hiren Patel

Reputation: 52800

Read Text from File:

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

This function will return String, use it as your requirement.

Use as below:

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

Done

Upvotes: -2

Related Questions