I want the user to choose some .txt file from device and then my app to store the copy of it inside its assets folder.
buttonChooseFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent txtIntent = new Intent(Intent.ACTION_GET_CONTENT);
txtIntent.setType("text/plain");
startActivityForResult(txtIntent, 1);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
File file = new File(data.getData().getPath());
chosenTxtFile = file;
}
}
}
When I run it I can indeed choose a file from my phone, but now when I want to execute the following code, nothing happens:
StringBuilder stringBuilder = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(chosenTxtFile));
String line;
while ((line = br.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
br.close();
}
catch (IOException e) {
}
textView.setText(stringBuilder.toString());
I read here: Android: Reading txt file using implicit intent that I should use ContentResolver. However since I'm new to this, I'm not exactly sure how. Could anyone please help me write ContentResolver that would work with my code? Much appreciated!