In my app I need to read a settings file, and that settings file can either be on local storage or on the user's Google Drive storage (with Google Drive app installed).
The following opens up a file chooser, first asking the user which file picker to use, including the option of using the Google Drive file picker:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);
In my onActivityResult()
function, if the user opted to use a file picker to choose a local file, then I already know how to successfully get the file path and read the file. But if the user instead used the Google Drive file picker to choose a remote file, how do I access the file that the user selected?
These are the bare bones of my onActivityResult()
function:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_FILE_PICKER && resultCode == RESULT_OK) {
if (LOCAL FILE) {
Uri uri = data.getData();
File myFile = new File(uri.getPath());
String filePath = myFile.getAbsolutePath();
// now read file store at 'filePath' from local storage (this part is fine)
} else if (GOOGLE DRIVE FILE) {
// what do I do here to retrieve the selected file?
}
}
super.onActivityResult(requestCode, resultCode, data);
}
When selecting a Google Drive file using the Google Drive file picker, the selected file seems to be downloaded (but to where??) but then nothing happens... how do I access the downloaded file to use in my app?
And in onActivityResult()
above, how do I tell if the selected file is actually a Google Drive file, so that I can treat it accordingly? i.e. what is the test for if (GOOGLE DRIVE FILE)
?
filePath
derived in the above code, even when a file from google drive is selected. In other words, if the user chooses a file picker that allows them to browse to google drive and select a file from there, the above code just seems to work... i.e. thefilePath
is valid and my method for reading the file works with local files and drive files. Maybe it depends on the file picker used... the one that's working for me is the stock picker on Galaxy S6. Sorry I can't be of more help.