Problem : I'm trying to open a txt file using implicit Intent(ACTION_GET_CONTENT)
and store the txt file's content into an arraylist. When I try to open the file with file path from Uri's getPath()
and create a BufferedReader
object to read from the text file, I get an error says that such file path does not exist.
In Logcat, it says my file path is "/document/1505-2A0C:Download/text.txt"
and when I try to open the file it says:
"W/System.err: java.io.FileNotFoundException:
/document/1505-2A0C:Download/text.txt: open failed:
ENOENT (No such file or directory)"
Here's my code:
@Override
public void onClick(View v) {
// Send implicit intent to load a file from directory
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
startActivityForResult(Intent.createChooser(intent,
"Load a file from directory"), REQUEST_CODE_SEARCH);
}
onActivityResult()
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_SEARCH) {
try {
Uri uri = data.getData();
String filepath = uri.getPath();
// In logcat : File path: /document/1505-2A0C:Download/text.txt
Log.d("File path", filepath);
File file = new File(filepath);
ArrayList<String> strings = new ArrayList<>();
/* Problem occurs here : I do not get correct file path to open a FileReader.
In logcat: "W/System.err: java.io.FileNotFoundException:
/document/1505-2A0C:Download/text.txt: open failed:
ENOENT (No such file or directory)"*/
BufferedReader br = new BufferedReader(new FileReader(file));
// Rest of code that converts txt file's content into arraylist
} catch (IOException e) {
// Codes that handles IOException
}
}
}
In summary : I get "/document/1505-2A0C:Download/text.txt"
for file path, and when I open the file in BufferedReader
using the file path, it says there is no such directory.
What am I doing wrong in here?