Intent.ACTION_GET_CONTENT with Google Drive
Asked Answered
R

1

7

I have the following intent:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent, DBOpenHelper.REQUEST_CODE_RESTORE);

The intent allows the user to select a text file from a number of options. It works fine with local storage and Dropbox for example, and in both cases I can get the file from as follows:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if ((requestCode == DBOpenHelper.REQUEST_CODE_RESTORE)
            && (resultCode == Activity.RESULT_OK)) {
        restoreFile = new File(data.getData().getPath());
        restoreFileName = restoreFile.getName();
    }
}

Local storage works fine and Dropbox will copy a local copy of the file to the SD card and return the correct path. The problem is that if the user to selects files from Google Drive. When they use Google Drive, data.getData().getPath() returns something like: "/document/acc=1;doc=195" instead of returning the path to the locally stored file. How do I have Google Drive download the file and return the path? I want to allow the user to select from any file storage option they have available.

Revulsion answered 4/1, 2015 at 22:19 Comment(0)
M
7

Google Drive may or may not have downloaded the file locally when the user picks the file. However, in all cases, you can access the contents of the file via getContentResolver().openInputStream(data.getData()) - note that openInputStream() also supports local files and can and should be used in other cases as well.

Marchand answered 4/1, 2015 at 22:26 Comment(1)
Thank you! This is much easier!Revulsion

© 2022 - 2024 — McMap. All rights reserved.