I am developing an app that consists of a button to read a file from android device with java on click and then I want to use that input file for further process. I want to store that file as string.
I tried several methods with InputStream and BufferedReader but none of them worked!
Below is my intent part to read text/ csv file:
//Button for choosing file from device
Button button = (Button) findViewById(R.id.button);
InputStream inputstream = null;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Onclick action of the button
Intent intent = new Intent(MainActivity.this, FilePickerActivity.class);
intent.putExtra(FilePickerActivity.CONFIGS,new Configurations.Builder()
.setCheckPermission(true)
.setShowFiles(true)
.setShowImages(false)
.setShowVideos(false)
.setMaxSelection(1)
.setSuffixes("txt","csv")
.setSkipZeroSizeFiles(true)
.build());
startActivityForResult(intent,102);
}
});
Then i used code below for getting path of file selected;
protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data!=null){
ArrayList<MediaFile> mediaFiles = data.getParcelableArrayListExtra(
FilePickerActivity.MEDIA_FILES
);
String path = mediaFiles.get(0).getPath();
TextView tv1 = (TextView) findViewById(R.id.textView9);
tv1.setText("File Path: "+path);
Toast.makeText(MainActivity.this, "File Selected", Toast.LENGTH_SHORT).show();
}
}
I want to store the content of this file chosen in a string str.