8

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

Chris Byatt
  • 3,689
  • 3
  • 34
  • 61
  • Did you try to check this, I see that you have white space in your filename http://stackoverflow.com/questions/10301674/save-file-in-android-with-spaces-in-file-name. – aleksamarkoni Jun 26 '15 at 09:31
  • @aleksamarkoni i've reencoded the url with %20 instead of a " " but still FileNotFoundException – Chris Byatt Jun 26 '15 at 09:39
  • Can you please check if SD card is available at the moment of trying. http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage – aleksamarkoni Jun 26 '15 at 09:44
  • and also please check this http://stackoverflow.com/questions/14488794/opening-a-local-android-file-with-spaces – aleksamarkoni Jun 26 '15 at 09:46
  • @aleksamarkoni the external storage is available and replacing " " with "\\ " didn't help – Chris Byatt Jun 26 '15 at 09:51
  • 6
    use `InputStream is = getContentResolver().openInputStream(uri)` to get the content. See http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream%28android.net.Uri%29 – Andrei Mărcuţ Jun 26 '15 at 10:22
  • @Markus That's solved it, thanks – Chris Byatt Jun 26 '15 at 12:07

2 Answers2

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

        }
Ucdemir
  • 2,852
  • 2
  • 26
  • 44
-2

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

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • 1
    This obviously is _not_ the answer. The input stream should originate from Uri, not from File/Path. Especially in terms of the emphasis upon content Uri not file Uri by Google. – rommex Jul 20 '17 at 08:36