Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open asset file pdf in application

Tags:

android

pdf

I have searched already and not found any solution for show pdf file in application

The point is i have to show pdf from asset or raw folder not from web

I have already tried in webview with code

    WebView webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setContentDescription("application/pdf");
    webview.loadUrl("file:///android_asset/button11.pdf");

but its not woking.

i need help on that thanks in advance

like image 489
djk Avatar asked Nov 14 '22 00:11

djk


1 Answers

This is the code I use to read a pdf from the filesystem, I hope it helps!

        File file = new File("/path/to/file");          
        Uri path = Uri.fromFile(file);      
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
             context.startActivity(intent);
            } 
         catch (ActivityNotFoundException e) {
             Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
            }
like image 145
2bard Avatar answered Jan 03 '23 16:01

2bard