4

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)?

2
  • How did you solve it? Commented Sep 19, 2017 at 9:24
  • @NaveedAli hmm, at the moment it just seems to be working when I use the 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. the filePath 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. Commented Sep 19, 2017 at 9:53

1 Answer 1

0

I believe you're using OpenFileActivityBuilder when Drive is selected as the file picker. It will return a EXTRA_RESPONSE_DRIVE_ID which is the Drive ID of the selected file. After this, you just need to call DriveFile.open to open the said file. This can be your flag to check if you selected a Drive File picker or not.

I cannot answer where the file is downloaded, but my best guess for it is its inside your data/ of the application (which you most likely don't have access to)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.