27 Jan 2014

Open/Read PDF file in Android

Hello Friends,

Today i am going to post pdf view sample. There are many ways to open PDF file in android application.



Solution 1 : View PDF  using external application like adobe reader.  

here i have already created on directory(pdf_Writer_Reader) in SDcard and it contain one PDF file, which i am going to open in PDFviewr application.

import java.io.File;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
 Button btnOpenPDF;
 public String Directory = Environment.getExternalStorageDirectory() + "/pdf_Writer_Reader/"; 

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  btnOpenPDF=(Button) findViewById(R.id.btnViewPDF);

  btnOpenPDF.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    // Just Pass here PDF name which you want to open..
    OpenPDF openPDF = new OpenPDF("Impact  Brochure");
    openPDF.execute();

   }
  });
 }

 private class OpenPDF extends AsyncTask<Uri, Void, Void>
 {

  ProgressDialog pdgs = null;
  private Uri path = null;
  String filename = "";

  public OpenPDF(String filename)
  {
   this.filename = filename;

  }

  @Override
  protected void onPreExecute() {

   pdgs = ProgressDialog.show(MainActivity.this, "","Please wait...", true, false);
  }

  @Override
  protected Void doInBackground(Uri... params)
  {     

   File file=new File(Directory+filename+".pdf");
   if(file.exists())
   {
    path = Uri.fromFile(file);
    Log.e("Pdf Path=",""+path);
   }
   return null;
  }

  @Override
  protected void onPostExecute(Void unused)
  {
   if (pdgs != null && pdgs.isShowing())
    pdgs.dismiss();

   try
   {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "application/pdf");
    try
    {
     startActivity(intent);

    }
    catch(ActivityNotFoundException e)
    {
     Toast.makeText(MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
    }


   } catch (Exception e)
   {
    e.printStackTrace();
   }
  }
 }
}

Solution 2 : Read/Write PDF  within application.  

here below is the library in which you will achieve read/write PDF file within application.
Download

Note: Do not forgot to add below  permission in android Manifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

No comments:

Post a Comment