0

I have already searched a lot on this topic but I could not able to find the proper answer. I kindly request to not mark it with duplication without answer.

I have two activities in my android application.

1]. List of PDF files available on server on click of any of that file start downloading and show progress bar till the time downloading is finished. After downloading finish pop up with two button [view] or [cancel] is opened. on click of View open second activity passing pdf file path as a parameter.

2]. Inside this second activity I want to open PDF file passed with intent in my application's activity only.

I don't want to use any brower , drive , file manager or any external android application.

How can I achieve this, Please provide me appropriate solution. Thank you in advance :)

jil123
  • 99
  • 1
  • 12
  • did you check pdf viewer library? https://github.com/barteksc/AndroidPdfViewer – Youngjae Jul 07 '16 at 11:15
  • Use intent to open that PDF file and pass that file in intent as below, Hope this will help you to sort out your problem, It works for me.... private void viewPdf(File myFile){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(myFile), "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); } This code will open PDF file in default PDF file viewer or will list out various PDF viewer. – Bhavnik Jul 07 '16 at 11:28
  • Have you tried/applied my updated answer. – Jitesh Prajapati Jul 08 '16 at 05:42
  • Thank you @Youngjae I have checked it and this is exactly I what I want, it works for me. :) – jil123 Jul 08 '16 at 06:28
  • Yes, @userJP I have checked it and it works fine. Thanks a lot for your support. :) – jil123 Jul 08 '16 at 06:30

1 Answers1

2

Send file name from FirstActivity to SecondActivity.

Intent i=new Intent(FirstActivity.this, SecondActivity.class);
 i.putExtra("file", fileUrl);
 startActivity(i);

in SeconActivity, Use WebView

EDIT

Intent i = getIntent();
    String myPdfUrl = i.getStringExtra("file");
    String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
    String doc="<iframe src='"+url+"' width='100%' height='100%' style='border: none;'></iframe>";
    WebView web=(WebView)findViewById(R.id.webViewpdf);
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadData(doc, "text/html", "UTF-8");

layout.xml

<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webViewpdf"/>

</LinearLayout>
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
  • Thank you @userJP but this code providde options for opening PDF file in Drive or Browser(Chrome, firefox,etc.). I want to open inside the SecondActivity only. – jil123 Jul 07 '16 at 12:47
  • Thanks userJP @Youngjae my concept need both of yours answers. – jil123 Jul 08 '16 at 06:32