Android:读取Google云端硬盘文件选择器中选定的文件 [英] Android: reading file selected in Google Drive file picker

查看:261
本文介绍了Android:读取Google云端硬盘文件选择器中选定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要阅读设置文件,并且该设置文件可以位于本地存储上,也可以位于用户的Google云端硬盘存储空间中(安装了Google云端硬盘应用)。



下面打开一个文件选择器,首先询问用户使用哪个文件选择器,包括使用Google Drive文件选择器的选项:

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType(file / *);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent,打开文件);
startActivityForResult(chooserIntent,REQUEST_CODE_FILE_PICKER);

在我的 onActivityResult()函数中,if用户选择使用文件选择器来选择本地文件,然后我已经知道如何成功获取文件路径并读取文件。但是,如果用户使用Google云端硬盘文件选择器来选择远程文件,那么我如何访问用户选择的文件?

这些是我的 onActivityResult()函数的基础:

  @覆盖
protected void onActivityResult(int requestCode,int resultCode,Intent data){

if(requestCode == REQUEST_CODE_FILE_PICKER&& resultCode == RESULT_OK){

如果(本地文件){

Uri uri = data.getData();
文件myFile =新文件(uri.getPath());
String filePath = myFile.getAbsolutePath();
//现在从本地存储中读取'filePath'处的文件存储(这部分是好的)

}其他if(GOOGLE DRIVE FILE){
//我该怎么办在这里检索选定的文件?
}

}

super.onActivityResult(requestCode,resultCode,data);
}

使用Google云端硬盘文件选择器选择Google云端硬盘文件时,似乎被下载(但到哪里??),但然后没有任何反应......我如何访问下载的文件在我的应用程序中使用?



在上面的 onActivityResult()中,我如何判断所选文件是否实际上是Google Drive文件,以便我可以相应地对待它?即对如果(GOOGLE DRIVE FILE)

解决方案

我相信你正在使用 <$当驱动器被选为文件选择器时,c $ c> OpenFileActivityBuilder 。它将返回一个 EXTRA_RESPONSE_DRIVE_ID ,它是所选文件的驱动器ID。之后,您只需调用 DriveFile.open 即可打开该文件。这可以是您的国旗来检查您是否选择了驱动器文件选择器。



我无法回答文件的下载位置,但我最好的猜测是它的内部您的数据/ 应用程序(您很可能无法访问)


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

解决方案

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)

这篇关于Android:读取Google云端硬盘文件选择器中选定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆