I am planned to develop an App with very simple concept.The requirement is I want to add a text file from phone(Dynamic, so set span with clickabble position not possible for spannable string) with help of intent chooser. And need to display the selected file content in textView (any view if u suggested). Once I clicked any of the word from textview content i need display that word in Toast.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showFileChooser();
}
});
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType("*/*"); //all files
intent.setType("text/xml"); //XML file only
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 1);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// User pick the file
Uri uri = data.getData();
ExternalFileHandling fileObj=new ExternalFileHandling(getApplicationContext());
String fileContent = fileObj.readTextFile(uri);
aboutTextView.setText(fileContent);
Toast.makeText(this, fileContent, Toast.LENGTH_LONG).show();
} else {
Log.i("data", data.toString());
}
}``
public String readTextFile(Uri uri){
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(Currentcontext.getContentResolver().openInputStream(uri)));
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return builder.toString();
}
[![intent chooser for dynamic file content][1]][1]