2

I have created a app which works like if user clicks on any PDF it will download that PDF. Now I want to display that PDF after the download. i don't known how I can display that pdf after the download on onPostExecute method. Storage path of file is Environment.getExternalStorageDirectory() + "/" + "android" + "/" + "Data" + "/"+ "foldername"+"/"+"Filename"

public class DownloadTask {
cutomealertbox cdd;
File apkStorage = null;
File outputFile = null;
private Context context;

private String downloadUrl = "", downloadFileName = "";
public DownloadTask(Context context, String downloadUrl) {
    this.context = context;

    this.downloadUrl = downloadUrl;
    downloadFileName = downloadUrl.substring(downloadUrl.lastIndexOf('/'), downloadUrl.length());
    Log.e(TAG, downloadFileName);
    cdd=new cutomealertbox((Activity) context);
    new DownloadingTask().execute();
}

private class DownloadingTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cdd.show(); // cdd is alert box
    }

    @Override
    protected void onPostExecute(Void result) {
        try {
            if (outputFile != null) {
                cdd.dismiss();
            } else {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                    }
                }, 3000);

                Log.e(TAG, "Cannot load please try again");

            }
        } catch (Exception e) {
                Log.e(TAG, "Download Failed with Exception - " + e);

        }
Ajeet Jain
  • 47
  • 7

3 Answers3

1

Use below method and pass context and local path as argument.

public static void openFile(Context context, String localPath) {
    // Create URI
    try {
        File file = new File(localPath);

        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
        if (file.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }


    } catch (Exception e) {
        e.printStackTrace();
    }
}
Karthik Thunga
  • 1,093
  • 2
  • 12
  • 21
  • I don't want to open on Default pdf Reader i have created a pdf Viewer using git hub Library So how i can open that pdf on that viewer when i jump from download task to pdf viewer it closed the app so how i can open the pdf on PDF Viewer – Ajeet Jain Jul 30 '18 at 11:38
0

On Post Execute ,

Using Kotlin,

var extension: String? = null

        val file = File(Environment.getExternalStorageDirectory().toString() + "/" + "android" + "/" + "Data" + "/"+ "foldername"+"/"+"Filename")

        val builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        val uri = Uri.fromFile(file)

        val intent = Intent(Intent.ACTION_VIEW)
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
         if (uri.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf")
        } 
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        mContext!!.startActivity(intent)

By using Java

 String extension = null;

        File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "android" + "/" + "Data" + "/" + "foldername" + "/" + "Filename")

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        Uri uri = Uri.fromFile(file);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
        if (uri.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);
Shiva Snape
  • 544
  • 5
  • 9
  • But i want to view in pdf Viewer (Git Hub ) Library when i tried to jump from this activity to that activity it close the app now how i can view pdf in PDF Viewer library **'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'** – Ajeet Jain Jul 30 '18 at 11:31
  • it is fully documented and also sample application is there in github. Read that thoroughly. – Shiva Snape Jul 31 '18 at 03:43
  • also first u didn't mention that you don't need to open it using default pdf viewer – Shiva Snape Jul 31 '18 at 05:37
0

Assuming you have already integrated the library in your project. Add this to your XML layout in which you want to view the PDF and set the visibility to GONE.

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

In postExecute(Void result) method set the pdf view according to documentation given here, and set the visibility of pdfView to View.VISIBLE.

Nainal
  • 1,728
  • 14
  • 27