0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Abhishek Patel
  • 121
  • 1
  • 1
  • 11

1 Answers1

2

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.

<com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

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

MAIN CODE:

    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<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {
            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }

Thats it!

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

JexSrs
  • 155
  • 6
  • 18