I want to open .pdf file in my application

I want to open a document file in Android application with help of Android studio. How can be possible? Should I need to use web view? I had try source code from many webs but file was opened by other application

38.1k 21 21 gold badges 86 86 silver badges 119 119 bronze badges asked Feb 23, 2018 at 11:40 Abhishek Patel Abhishek Patel 127 1 1 gold badge 1 1 silver badge 12 12 bronze badges show your code what you tried. Commented Feb 23, 2018 at 11:42 You can use a library for it too.. github.com/barteksc/AndroidPdfViewer Commented Feb 23, 2018 at 11:45 You need to integrate MuPDf in your application. Take a look at here Commented Feb 23, 2018 at 11:46 Commented Feb 23, 2018 at 11:51 show what you have tried. Commented Feb 23, 2018 at 12:06

1 Answer 1

You can open pdf files using Intents.

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); 

If you dont like this method, and want to open pdf files inside your application, you can use a custom PDF Viewer.

In your gradle file compile this: 'com.github.barteksc:android-pdf-viewer:2.0.3'

After you sync your project, go to your xml file and add the PDF Viewer.

Now in your .java file you will import:

import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.github.barteksc.pdfviewer.PDFView; import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener; import com.github.barteksc.pdfviewer.listener.OnPageChangeListener; import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle; import com.shockwave.pdfium.PdfDocument; import java.util.List; 

You will Implement two methods: OnPageChangeListener and OnLoadCompleteListener

 public static final String SAMPLE_FILE = "android_tutorial.pdf"; //your file path PDFView pdfView; Integer pageNumber = 0; String pdfFileName; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pdfView= (PDFView)findViewById(R.id.pdfView); displayFromAsset(SAMPLE_FILE); >private void displayFromAsset(String assetFileName) < pdfFileName = assetFileName; pdfView.fromAsset(SAMPLE_FILE) .defaultPage(pageNumber) .enableSwipe(true) .swipeHorizontal(false) .onPageChange(this) .enableAnnotationRendering(true) .onLoad(this) .scrollHandle(new DefaultScrollHandle(this)) .load(); >@Override public void onPageChanged(int page, int pageCount) < pageNumber = page; >@Override public void loadComplete(int nbPages) < PdfDocument.Meta meta = pdfView.getDocumentMeta(); printBookmarksTree(pdfView.getTableOfContents(), "-"); >public void printBookmarksTree(List tree, String sep) < for (PdfDocument.Bookmark b : tree) < if (b.hasChildren()) < printBookmarksTree(b.getChildren(), sep + "-"); >> > 

P.S: First search on google, if you dont find something, write your question!