Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make pdf viewer function in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 147
    Comment on it

    Using the below code I have created pdf viewer function. Here I have created pdf Viewer Activity and used string download_token,publication_heading,download_info and pdf data fetch data in DatabaseHelper. I have also used DownloadManger fuction. Below code will clearly described you to make pdf viewer .

    Step(1)Create PdfViewerActivity-

    public class PdfViewerActivity extends AppCompatActivity {
        private WebView wvPdfShow;
        private String download_token, publication_heading, download_info;
        private DataBaseHelper mydatabase;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pdf_viewer);
    
            Intent intent = getIntent();
            download_token = intent.getStringExtra("articlepdf_token");
            publication_heading = intent.getStringExtra("publication_heading");
            download_info = intent.getStringExtra("download_info");
            System.out.println("download token check...." + download_token);
    
            wvPdfShow = (WebView) findViewById(R.id.wvPdfShow);
            wvPdfShow.getSettings().setJavaScriptEnabled(true);
            wvPdfShow.setWebViewClient(new Callback());
    
            String urlEncoded = null;
            try {
                urlEncoded = URLEncoder.encode(download_token, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
            wvPdfShow.loadUrl("https://docs.google.com/viewer?url=" + urlEncoded);
            wvPdfShow.getSettings().setBuiltInZoomControls(true);
    
    
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        public void downloadurl(String url, String article_heading) {
    
            File direct = new File(Environment.getExternalStorageDirectory() + "/" + getString(R.string.app_name));
    
            if (!direct.exists()) {
                direct.mkdirs();
            }
    
            DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadUri = Uri.parse(url);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    
            System.out.println("Request...." + request);
    
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                    .setDescription("Downloading pdf " + article_heading)
                    .setDestinationInExternalPublicDir("/" + getString(R.string.app_name), article_heading + ".pdf");
    
            registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    
            mgr.enqueue(request);
        }
    
        BroadcastReceiver onComplete=new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                // Do Something
                Toast.makeText(PdfViewerActivity.this, getResources().getString(R.string.article_sucessfully_saved), Toast.LENGTH_LONG).show();
            }
        };
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_pdf_viewer, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            if (id == android.R.id.home) {
                finish();
            }
            if (id == R.id.download) {
                mydatabase = new DataBaseHelper(PdfViewerActivity.this);
    
                //if (mydatabase.unique(article_heading).equalsIgnoreCase(article_heading))
                if (mydatabase.unique(publication_heading) > 0) {
    
                    Toast.makeText(PdfViewerActivity.this, getResources().getString(R.string.article_alreadysaved), Toast.LENGTH_LONG).show();
                } else {
                    mydatabase.insertRecord(publication_heading, download_info);
    
                    downloadurl(download_token, download_info);
                }
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        private class Callback extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return (false);
            }
        }
    }
    

    Step(2)-activity_pdf_viwer.xml layout-

    <RelativeLayout
        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"
        tools:context="com.pantheon.android.ui.PdfViewerActivity">
    
        <WebView
            android:id="@+id/wvPdfShow"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </WebView>
    
    </RelativeLayout>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: