Aakash Jain
Aakash Jain

Reputation: 47

How can I read the file after the download and display in pdf viewer in android

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);

        }

Upvotes: 2

Views: 2073

Answers (3)

Nainal
Nainal

Reputation: 1748

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.

Upvotes: 0

Karthik Thunga
Karthik Thunga

Reputation: 1103

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();
    }
}

Upvotes: 1

Shiva Snape
Shiva Snape

Reputation: 544

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);

Upvotes: 0

Related Questions