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?
In android, we can lunch other applications using packing name. This example demonstrate about How to Launch an application from another application on Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
You can upload your apk to Google Drive, right click file on Google Drive -> share this file to your client. Then after your client download successfully, you can delete this file from your Google Drive for safe.
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:
For gmail sharing we need to use following type of code:
Uri zipUri = Uri.parse("file://sdcard/sdcard0/test.zip");
String[] emailArr = {"[email protected]"};
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With