How to receive pdf file sent or shared from other applications?


Hi i am learning android application development. What i am stuck in my current project is i don know how to receive pdf file sent or shared from other applications. I see many apps can do this task. Please teach me...

By chen.davy asked on 2015-08-31
answer to this question

yuk.dara
By yuk.dara

Ok, first you have to add read external storage permission in androidmanifest.xml file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Then in the androidmanifest file in the Activity tag, you add an intent-filter as shown below. There are two actions are specified. The first action (SEND) allows your app to receive a pdf file sent from another application. The second action (VIEW) allows the application to view receive a pdf file that is selected from file explorer. You need to put "application/pdf" as mine type.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType= "application/pdf" />
<data android:host="*" />
</intent-filter>

Finally in your code, you get path of the pdf file as shown below:
Intent intent=getIntent();
if(intent!=null) {
String action=intent.getAction();
String type=intent.getType();
if(Intent.ACTION_VIEW.equals(action) && type.endsWith("pdf")) {
// Get the file from the intent object
Uri file_uri=intent.getData();
if(file_uri!=null)
filepath=file_uri.getPath();
else
filepath="No file";
}
else if(Intent.ACTION_SEND.equals(action) && type.endsWith("pdf")){
Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
filepath = uri.getPath();
}


2015-08-31 Reply





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.