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
Asked
Active
Viewed 1.6k times
0
-
4show your code what you tried. – Vikas singh Feb 23 '18 at 11:42
-
You can use a library for it too.. https://github.com/barteksc/AndroidPdfViewer – Santanu Sur Feb 23 '18 at 11:45
-
You need to integrate MuPDf in your application. Take a look at [here](https://stackoverflow.com/questions/8500530/integrate-mupdf-reader-in-an-app/8587527#8587527) – ahmetsarias Feb 23 '18 at 11:46
-
https://commonsware.com/blog/2017/01/04/options-viewing-pdfs.html – CommonsWare Feb 23 '18 at 11:51
-
show what you have tried. – Vladyslav Matviienko Feb 23 '18 at 12:06
-
read answer here: https://stackoverflow.com/a/27820255/12980819 – Falchio Oct 04 '22 at 14:00
1 Answers
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
-
Thanks,Great Work. but I am accessing pdf files from server. So how can i open it with URL? – Abhishek Patel Feb 27 '18 at 07:30
-
I am not sure but you can write in `SAMPLE_FILE` the url of pdf you want _e.x. https://www.mywebsite.com/pdfs/myFile.pdf_ – JexSrs Feb 27 '18 at 07:34