我尝试通过手动启动 Google 驱动器(安装在设备上)从 Android 应用程序上传文件。我尝试使用此发送Intent.createChooser
其上传文件附件工作正常。但我需要出于特定目的上传文件(例如 Dropbox、仅限 Google Drive)。所以我更改了代码并尝试按照以下方式将文件上传到Google Drive,但没有成功,只有Google Drive应用程序在设备上打开,没有上传文件:
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文件吗?
经过研究,我得到了执行以下代码的解决方案:
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);
同样,我们可以用于其他共享意图,几个意图对应的包名称如下:
- com.dropbox.android = Dropbox
- com.android.bluetooth = 蓝牙
- com.android.email = 电子邮件
- com.google.android.gm = Gmail
- com.microsoft.skydrive = Skydrive
- com.google.android.apps.docs = Google 云端硬盘
对于 Gmail 共享,我们需要使用以下类型的代码:
Uri zipUri = Uri.parse("file://sdcard/sdcard0/test.zip");
String[] emailArr = {"[email protected] /cdn-cgi/l/email-protection"};
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)