使用Intent从Google驱动器获取文件

寿司

我正在尝试从我的应用程序上传文档。

一切正常,但当我从驱动器中选择文件时。

data=Intent { act=android.intent.action.VIEW dat=content://com.google.android.apps.docs.storage.legacy/enc=ckpgt5KcEEF_JYniJQafRV_5pEnu_D5UAI1WF-Lu6h2Z_Vw4
     (has extras) }}

任何机构都可以知道如何处理此文件。

我已经处理过所有文件和图像,但仅遇到Google驱动器文件问题。

我正在获取content://com.google.android.apps.docs.storage.legacy/enc=ckpgt5KcEEF_JYniJQafRV_5pEnu_D5UAI1WF-Lu6h2Z_Vw4意图数据Uri。

维卡斯

Uri通过文件选择器选择Google-Drive文件收到的句柄

如前所述,它收到Virual File Uri

我发现此示例代码简单易懂。给定的代码示例对我有用。希望它适用于您的情况。

1.因此检测到该uri是Google驱动器收到的。

public static File getFileFromUri(final Context context, final Uri uri) throws Exception {

if (isGoogleDrive(uri)) // check if file selected from google drive
{ 
  return saveFileIntoExternalStorageByUri(context, uri);
}else 
    // do your other calculation for the other files and return that file
   return null;
}


public static boolean isGoogleDrive(Uri uri)
{
 return "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
}

2.如果是,则将uri存储到外部路径(在此处可以根据需要更改其根目录),并创建带有该uri的文件。

 public static File saveFileIntoExternalStorageByUri(Context context, Uri uri) throws 

Exception {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        int originalSize = inputStream.available();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String fileName = getFileName(context, uri);
        File file = makeEmptyFileIntoExternalStorageWithTitle(fileName);
        bis = new BufferedInputStream(inputStream);
        bos = new BufferedOutputStream(new FileOutputStream(
                file, false));

        byte[] buf = new byte[originalSize];
        bis.read(buf);
        do {
            bos.write(buf);
        } while (bis.read(buf) != -1);

        bos.flush();
        bos.close();
        bis.close();

        return file;

    }

public static String getFileName(Context context, Uri uri) 
{
  String result = null;
  if (uri.getScheme().equals("content")) {
  Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
  try {
       if (cursor != null && cursor.moveToFirst()) {
        result = cursor.getString(cursor.getColumnInde(OpenableColumns.DISPLAY_NAME));
       }
      } finally {
                cursor.close();
            }
        }
        if (result == null) {
            result = uri.getPath();
            int cut = result.lastIndexOf('/');
            if (cut != -1) {
                result = result.substring(cut + 1);
            }
        }
        return result;
}


public static File makeEmptyFileIntoExternalStorageWithTitle(String title) {
        String root =  Environment.getExternalStorageDirectory().getAbsolutePath();
        return new File(root, title);
    }

注意:此处是从Intent中检索虚拟文件getData()并将其用于的context.getContentResolver().openInputStream(intent.getData()),这将返回InputStream从谷歌驱动器中获取所选文件的句柄。

有关更多信息,请通过此链接

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Google脚本获取驱动器中文件夹的文件夹ID?

Google Apps Script - 使用文件名获取驱动器中文件的 URL

Google脚本,获取驱动器文件网址

使用PHP API获取Google驱动器上的文件列表

使用Colab笔记本获取Google驱动器中文件的共享链接

如何使用使用Python的驱动器API将文件上传到Google驱动器?

在 Python 中使用 aiohttp 上传 Google 驱动器文件

Python:使用网址从Google驱动器下载文件

R如何使用R从Google驱动器读取文件

使用wget从Google驱动器下载文件

使用beautifulsoup将文件下载到Google驱动器

使用服务帐户密钥列出来自Google驱动器的公共共享文件,但未获取公共文件

获取驱动器的页面文件大小

如何通过文件夹名称或文件夹ID获取Google驱动器文件?

如何以编程方式获取Google驱动器文件的文件(视频)持续时间?

文件夹getParents无法在Google脚本中获取团队驱动器名称

通过名称搜索Google驱动器文件并获取其ID

如何通过API在Google驱动器中获取“与我共享”文件?

Google Drive API-Android-如何获取驱动器文件ID?

从Lollipop中的Google驱动器获取文件路径(MediaStore.MediaColumns.DATA == null)

使用Microsoft Graph API获取驱动器的所有SharePoint文件和文件夹

使用带有PyDrive的Python在Google驱动器上成功上传了文件,但文件已损坏

Google Team Drive使用Apps脚本在团队驱动器文件夹之间移动文件

使用Xamarin.Android将文件上传到Google驱动器文件夹

通过使用Google文档在Web视图中显示(存储在Google驱动器中的)PDF文件

使用google-colaboratory上传到google驱动器的文件的路径是什么?

Google Colab错误,无法使用Google驱动器中的文件

使用没有Google API的C#从Google驱动器下载文件

使用Google Apps脚本将文件从Google驱动器上传到外部API