I'm currently trying to write a program that can load a selected text file from internal/external storage for use in an Android application.
I've already tried loading the file using the following code via this page to no avail:
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"file.txt");
and this (thanks to Google)
public void performFileSearch() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("text/plain");
startActivityForResult(intent, READ_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
Log.i(TAG, "Uri: " + uri.getPath());
File file = new File(uri.getPath());
if (file.exists()) {
Log.i(TAG, "Exists");
} else {
Log.i(TAG, "Does not exist");
}
}
}
}
In the later case, (where the user selects a text file) when a text file is selected it surprisingly returns false. I also get similar errors for the first block of code. I read that you can load a file from a uri using an InputStream, but I don't know how to implement this. Thanks for any and all the help.