【问题标题】:Google Drive GET_CONTENT intent read fileGoogle Drive GET_CONTENT 意图读取文件
【发布时间】:2013-07-27 15:47:01
【问题描述】:

我在 android 上有一个应用程序,它使用 Dropbox 等云存储进行文件共享。要开始分享,我会抛出android.intent.action.SEND。 在显示的列表中,我看到了 Google Drive 应用程序(以前安装),所以我尝试将文件发送给它 - 它工作正常,文件出现在 Drive 列表中。

然后,我想在另一台设备上读取此文件。我扔 android.intent.action.GET_CONTENT意图,选择驱动器然后不知道如何获取文件。我收到一个类似这样的 Uri:

content://com.google.android.apps.docs.files/exposed_content/6jn9cnzdJbDywpza%2BlW3aA%3D%3D%0A%3BRV%2FaV94o%2FCcW4HGBYArtwOdHqt%2BrsYO4WmHcs6QWSVwp%2FXogkRAgit7prTnfp00a%0A

我不知道如何转换为物理文件路径。我如何从中获取文件内容?

我在内容提供者周围玩了一下,可以得到文件名,但不是完整路径或其他任何东西。

对于 Dropbox,我得到了 file:// 样式的 uri,简单明了,效果很好。

【问题讨论】:

    标签: android google-drive-api file-get-contents


    【解决方案1】:

    它正在向您发送内容提供者的 uri,您可以将其与 ContentResolver 一起使用,例如:

    getContentResolver().query(Uri contentUri, String[] projection, String selection, String[] selectionArgs, String sortOrder);
    

    编辑:要获取真实路径名,请使用下面提供的解决方案

    Android: Getting a file URI from a content URI?

    【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。我使用以下代码从 Google Drive 文件中获取文件路径。它适用于 SkyDrive 和 DropBox。

    String filePath = null;
    Uri _uri = data.getData();
    Log.d("", "URI = " + _uri);                                       
    if(_uri != null && "content".equals(_uri.getScheme()))  {
        Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Files.FileColumns.DATA }, null, null, null);
        cursor.moveToFirst();   
        filePath = cursor.getString(0);
        cursor.close();
    } else {
         filePath = _uri.getPath();
    }
    Log.d("", "Chosen path = " + filePath);
    

    我正在使用意图来选择文件。这是我选择文件的代码。

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    String strType = "*/*";
    intent.setDataAndType(Uri.parse(dir.getAbsolutePath()), strType);
    startActivityForResult(intent, PICKFILE_RESULT_CODE);
    

    当我从内部或外部存储器获取文件时,我的代码工作正常。我想用相同的代码从 Google Drive 获取文件。

    【讨论】:

    • 您是否发现从谷歌驱动器下载的问题?
    • 无法获取原始文件路径。
    • Jahangeer Ahmed 什么是目录?在您的代码片段中选择文件
    • 这是一个目录 Radoslav
    猜你喜欢
    • 2016-01-26
    • 2020-09-17
    • 2017-08-03
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2019-10-29
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多