[Solved] Open PDF Automatically After Downloaded


Thanks to @blackapps for the suggestion.

I found out that the answer of my silly question is quite simple.

I insist in using URLUtil.guessFileName(url,contentDisposition,mimeType)) because when I download the pdf file, the downloaded filename will be the same with the uploaded filename in my client’s website.

So what I just did is adding this equation fileName = URLUtil.guessFileName(url,contentDisposition,mimeType)) and register receiver in Download Manager then create the reaction after pdf download is completed.

public class WebViewActivity extends AppCompatActivity {

...

    private String fileName;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ...
        
        webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition,
                                    String mimeType, long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            //setup the fileName
            fileName = URLUtil.guessFileName(url,contentDisposition,mimeType);
            
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie",cookies);
            request.addRequestHeader("User-Agent",userAgent);
            request.setDescription("Downloading File");
            request.setTitle(fileName));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                    fileName);
            DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            //Registering receiver in Download Manager
            registerReceiver(onCompleted, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    
            downloadManager.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_SHORT).show();
        }
        
    });
    
    ...
    
    BroadcastReceiver onCompleted = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context.getApplicationContext(), "Download Finish", Toast.LENGTH_SHORT).show();
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + fileName);
            Uri uri = FileProvider.getUriForFile(WebViewActivity.this, "com.example.app"+".provider",file);
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(uri, "application/pdf");
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(i);
        }
    };
}

solved Open PDF Automatically After Downloaded