Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.ACTION_GET_CONTENT with Google Drive

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.

like image 512
goodnoodle Avatar asked Jan 04 '15 22:01

goodnoodle


People also ask

How do I integrate with Google Drive?

From Android Studio, select Start a new Android Studio project from the startup screen or New Project from the File menu. Enter the name GoogleDriveDemo, your company domain (or example.com if your wish) and a project location. Make sure that Kotlin support is selected and then press Next.

What does Android intent action view do?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.


1 Answers

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.

like image 92
ianhanniballake Avatar answered Sep 20 '22 13:09

ianhanniballake