【问题标题】:File View from Google Drive Android Intent来自 Google Drive Android Intent 的文件视图
【发布时间】:2013-06-07 20:31:14
【问题描述】:

如果有人可以提供帮助,我会非常棒。我正在构建一个应用程序,我试图访问我的文件并将它们显示在图像视图中。

我有一个按钮,并在其上附加了一个 onClickListener

iButton.setOnClickListener(new View.OnClickListener() {   
@Override
public void onClick(View view) {
   Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
   photoPickerIntent.setType("image/*");
   startActivityForResult(Intent.createChooser(photoPickerIntent, "Select Picture"), 1);
   }
 });

intent 给了我 3 个选项 Gallery、Dropbox 和 Google Drive

对于图库,我可以访问该文件并将其显示在图像视图中

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null,   null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageHolder.setImageBitmap(BitmapFactory.decodeFile(picturePath));

对于 Dropbox,我是这样做的

imageHolder.setImageBitmap(BitmapFactory.decodeFile(selectedImage.getPath()));

但是我不确定如何为谷歌驱动器执行此操作,我尝试像画廊一样执行此操作,但出现以下错误

E/BitmapFactory: 无法解码流: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)

任何帮助将不胜感激。

【问题讨论】:

  • 到目前为止我发现:返回的 Uri 格式为:content://com.google.android.apps.docs.files/exposed_content/[base64-encoded-bytes]。 [base64-encoded-bytes] 实际上是一个反向的、换行和分号分隔的 URL 编码字符串,例如。 123...%3D%3D%0A%3B456... 应该解码成 456...123...== Base64-decoding 456...123...== 会产生 64 个字节的数据,但我不明白这些字节是什么。这是我得到的 64 字节的示例:cl.ly/3Y2C1s0v3O0Q 在 HEX 中没有更多意义:goo.gl/7WV9K
  • 您是否尝试过任何官方文档? developers.google.com/drive/integrate-android-ui

标签: android google-drive-api


【解决方案1】:

这里最初给出了正确答案:Google Drive GET_CONTENT intent read file

解决方案是从 contentResolver 获取它为InputStream

我从任何地方获取图像的完整代码:

Uri selectedImage = imageReturnedIntent.getData();
if (selectedImage == null) {
    return;
}
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    mCurrentPhotoPath = cursor.getString(columnIndex);
    cursor.close();
}

if (TextUtils.isEmpty(mCurrentPhotoPath)) {
    mCurrentPhotoPath = selectedImage.getPath();
}
Bitmap bitmap = null;
if (imageReturnedIntent.getDataString() != null && imageReturnedIntent.getDataString().contains("docs.file")) {
    try {
        InputStream inputStream = getContentResolver().openInputStream(selectedImage);
        bitmap = BitmapFactory.decodeStream(inputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

if (bitmap == null) {
    bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
}

【讨论】:

  • Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 抛出异常。这不是解决方案。
  • 罗杰,我刚刚尝试了您的解决方案,它奏效了。我以前一直在使用 ContentResolver 来查询从 GET_CONTENT ACTION 返回的内容的文件路径,然后作为文件打开,该文件适用于除 Google Drive 之外的所有来源。谢谢!
  • “java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.docs.sync.filemanager.FileProvider from ProcessRecord{42348288 30992:com.app.app/未从 uid 10015 导出的 u0a10253} (pid=30992, uid=10253)" 被抛出?
  • @igordcard 我认为最好阅读 FileProvide 文档并探索源代码。
  • 这对我也不起作用。我得到相同的权限拒绝
【解决方案2】:

是的,这就像用头撞墙一样。由于某些驱动端 api idiosyncrasies,您可能会在使用 ACTION_GET_CONTENT 时遇到麻烦。我最终抛出了一个黑客从该上下文菜单启动并使用standard integration code。祝你好运!

【讨论】:

    【解决方案3】:

    目前您不能在 Google 云端硬盘中使用 ACTION_GET_CONTENT。谷歌在旧版本中错误地支持它。但是最新版本的 Drive (1.1.470.15) 意图已被禁用,并且 ACTION_GET_CONTENT 不会将 Drive 作为选项抛出。

    阅读下面的问题讨论。
    https://productforums.google.com/forum/#!topic/drive/siSKHXdE-ao/discussion

    【讨论】:

    • 我有 1.2.228.32,当我启动 ACTION_GET_CONTENT Intent 时会出现 Drive 选项。
    • 那么我猜谷歌的家伙必须照顾好这个。请为此在 google 上提出问题。
    【解决方案4】:

    如果您想打开从云端硬盘检索到的任何文件,则可以在使用 Google 云端硬盘应用程序的资源时轻松完成。首先从 Play Market Install Google Drive 安装 Google Drive 应用程序

    之后使用此代码打开您的文件。

       FileID=file.getId();//it is your file ID which you want to display
    
      String url = "https://docs.google.com/file/d/"+FileID;
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(url));
       startActivity(i);
    

    它将显示您的任何类型的文件(图像、pdf、doc、txt 等)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      相关资源
      最近更新 更多