6

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

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

Alec.
  • 5,371
  • 5
  • 34
  • 69
user2459928
  • 231
  • 1
  • 2
  • 6

1 Answers1

17

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 = Bluetooth
  • com.android.email = Email
  • com.google.android.gm = Gmail
  • com.microsoft.skydrive = Skydrive
  • com.google.android.apps.docs = Googledrive

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);
user2459928
  • 231
  • 1
  • 2
  • 6
  • getting this error. Unable to start activity ComponentInfo{com.example.driveshareintent/com.example.driveshareintent.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=application/pdf flg=0x80001 pkg=com.google.android.apps.docs (has clip) (has extras) } – Ravi May 09 '14 at 09:46
  • 1
    @Ravi: You have to have the application installed: so allways check for this exception! (or you used a not existing package name) – Roel Jan 27 '15 at 12:01
  • If i can upvote 100 times within an hour i will spend one hour for this answer..... awesome man – Khizar Hayat Jul 07 '17 at 13:38
  • Is it working in marshmallow and plus ? i am getting security exception can you please help – Khizar Hayat Jul 07 '17 at 14:06
  • I tried it will only work for android 6.0 and above, when you make your own provider. for others help you can see this https://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps/39619468#39619468 – Khizar Hayat Jul 07 '17 at 15:16
  • Is it possible to launch into Google Docs to use their scan feature ? – A.Sanchez.SD Mar 26 '21 at 14:40