Android 从另一个未上传文件的应用程序启动 Google Drive 应用程序 [英] Android Launch a Google Drive application from another application not uploaded file

查看:22
本文介绍了Android 从另一个未上传文件的应用程序启动 Google Drive 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过手动启动 Google 驱动器(安装在设备上)从我的 android 应用程序上传文件.我尝试使用 Intent.createChooser 发送它,并且它可以正常上传文件附件.但我需要上传特定意图的文件(如 Dropbox,仅限 Google 驱动器).所以我更改了代码并尝试通过以下方式将文件上传到谷歌驱动器,但没有成功,只有谷歌驱动器应用程序在设备上打开,没有文件上传:

I tried to upload a file from my android application by manually launching Google drive (installed on the device). I tried this to send using Intent.createChooser and its working fine for uploading file attachment. But I need to upload file for specific intent (like Dropbox, Google drive only). So I changed the code and tried to upload a file to Google drive as following ways, but no success, only Google drive app is open on device, no file uploaded:

PackageManager pm = this.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.google.android.apps.docs");
intent.setType("application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/sdcard0/test.pdf"));
intent.putExtra(Intent.EXTRA_SUBJECT, "attach a file test");
intent.addCategory(Intent.ACTION_ATTACH_DATA);
startActivity(intent);

我们可以像上面那样手动打开 Intent 来上传 PDF 文件吗?

Can we upload a PDF file by opening the intent manually as above?

推荐答案

经过研究,得到了执行以下代码的解决方案:

I got the solution for executing following code after research:

import android.support.v4.app.ShareCompat;

Uri pdfUri = Uri.parse("file://sdcard/sdcard0/test.pdf");                
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
                                     .setText("Share PDF doc")
                                         .setType("application/pdf")
                                         .setStream(pdfUri )
                                         .getIntent()
                                 .setPackage("com.google.android.apps.docs");
startActivity(shareIntent);

同样我们可以用于其他的分享意图,几个意图对应的包名如下:

Similarly we can use for other share intent and the corresponding package name of few intents are as below:

  • com.dropbox.android = Dropbox
  • com.android.bluetooth = 蓝牙
  • com.android.email = 电子邮件
  • com.google.android.gm = 邮箱
  • com.microsoft.skydrive = Skydrive
  • com.google.android.apps.docs = Googledrive

对于 gmail 共享,我们需要使用以下类型的代码:

For gmail sharing we need to use following type of code:

Uri zipUri = Uri.parse("file://sdcard/sdcard0/test.zip");  
String[] emailArr = {"test@gmail.com"};              
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
                                  .setText("Share ZIP doc")
                                  .setType("application/zip")
                                  .setEmailTo(emailArr)
                                  .setStream(zipUri)
                                  .setSubject("Share zip doc")
                                  .setText("Sent with email app.")
                                  .getIntent()
                           .setPackage("com.google.android.gm");
startActivity(shareIntent);

这篇关于Android 从另一个未上传文件的应用程序启动 Google Drive 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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