【问题标题】:Android Launch a Google Drive application from another application not uploaded fileAndroid 从另一个未上传文件的应用程序启动 Google Drive 应用程序
【发布时间】:2013-06-02 14:05:20
【问题描述】:

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

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 文件吗?

【问题讨论】:

    标签: android-intent google-drive-api


    【解决方案1】:

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

    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 = Googledrive

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

    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);
    

    【讨论】:

    • 收到此错误。无法启动活动 ComponentInfo{com.example.driveshareintent/com.example.driveshareintent.MainActivity}:android.content.ActivityNotFoundException:未找到处理 Intent { act=android.intent.action.SEND typ=application/pdf flg= 的活动0x80001 pkg=com.google.android.apps.docs(有剪辑)(有额外内容)}
    • @Ravi:你必须安装应用程序:所以总是检查这个异常! (或者你使用了一个不存在的包名)
    • 如果我能在一小时内投票 100 次,我会花一个小时来回答这个问题.....真棒人
    • 它在棉花糖和加号中工作吗?我遇到了安全异常,请您帮忙
    • 我试过它只适用于 android 6.0 及更高版本,当您创建自己的提供程序时。对于其他人的帮助,你可以看到这个stackoverflow.com/questions/18249007/…
    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多