java - Android Read Text File from URI -


i've got uri pointing text file intent , i'm trying read file parse string inside it. i've tried it's failing filenotfoundexception. tostring() method appears lose /

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 = new fileinputstream(f); // fails on 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 value of data.getpath() is

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

i'm trying file directly uri rather path:

uri data = getintent().getdata(); string text = data.tostring(); //... file f = new file(text); 

but f appears lose 1 of slashes content://

f:

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

read text 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 {         = inputstream.read();         while (i != -1) {             bytearrayoutputstream.write(i);             = inputstream.read();         }             inputstream.close();         } catch (ioexception e) {             e.printstacktrace();         }     return bytearrayoutputstream.tostring(); } 

this function return string, use requirement.

use below:

log.i("text file", readtext()); 

done


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -