Reputation: 195
I am new to development with android, i am recently working with an android application that exports document into PDF (conversion) tool , the problem is after exporting of PDF i want to give a option to user to share the document(PDF) through intent, i have digged around stackoverflow but was not able to understand and answers were not actually answering my question. The PDF is exported/created into external sd card.
I have created a PDF through my application after exporting/creating the PDF i want to share them through intent , i have digged around stackoverflow but dint get answer.how can i share it through intent, like i share with image,text through intent.
Upvotes: 1
Views: 247
Reputation: 6067
Answer of user @oleonardomachado is correct but from android N update direct uri share is prohibited. you have to use file provider to get uri data and then share.
Share using intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= 24) {
Uri fileUri = FileProvider.getUriForFile(getContext(), getPackageName()+".fileprovider", file); // provider app name
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
shareIntent.setType("application/pdf");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("application/pdf");
}
startActivity(Intent.createChooser(shareIntent, "Share PDF"));
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
And after that create a file_paths.xml file in res/xml folder. xml Folder may be not there so create if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".").
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Hope this will help others.
Upvotes: 1
Reputation: 104
You can use ACTION_SEND to activate the chooser for the specified file type, just remember to provide "application/pdf" as the file type.
public void SharePdf(File file) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("application/pdf");
startActivity(Intent.createChooser(shareIntent, "Share PDF"));
}
Now call SharePdf(new File(fileName))
to start the intent and let the user select the correct option.
Upvotes: 0