Guest User

Untitled

a guest
Feb 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. PDFView pdfView; //pdfView object
  2. String URL;
  3. String fileName;
  4. File directory; //path of created File
  5. // Container for all parameters of DownloadAsync
  6. private static class AsyncParameters {
  7. String URL;
  8. File directory;
  9. AsyncParameters(String URL, File directory) {
  10. this.URL = URL;
  11. this.directory = directory;
  12. }
  13. }
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_pdf_view);
  19.  
  20. //Grab the extras from the intent call
  21. Intent intent = getIntent(); //whatever calls this activity, gather the intent
  22. URL = intent.getStringExtra("File URL"); // in this case, get the file name of the "extra" passed through
  23. fileName = intent.getStringExtra("File Name");
  24.  
  25. //Grab the internal storage directory and create a folder if it doesn't exist
  26. File intDirectory = getFilesDir();
  27. File folder = new File(intDirectory, "pdf");
  28. boolean isDirectoryCreated = folder.exists();
  29.  
  30. //See if the file exists
  31. if (!isDirectoryCreated) {
  32. isDirectoryCreated= folder.mkdir();
  33. }
  34. if(isDirectoryCreated) {
  35. directory = new File(folder, fileName);
  36. try {
  37. directory.createNewFile();
  38. if (directory.canWrite())
  39. Log.d("hngggggggggggggg", "onCreate: ");
  40. } catch (IOException e1) {
  41. e1.printStackTrace();
  42. }
  43. //See if file already exists
  44. boolean empty = directory.length() == 0;
  45. if (empty){
  46. /**Call class to create parameter container**/
  47. AsyncParameters param = new AsyncParameters(URL, directory);
  48. DownloadAsync Downloader = new DownloadAsync();
  49. Downloader.execute(param);
  50. showPdf();
  51. }
  52. else
  53. showPdf();
  54. }
  55.  
  56. }
  57. public void showPdf()
  58. {
  59. pdfView = (PDFView) findViewById(R.id.pdfView);
  60. pdfView.fromFile(directory).load();
  61. }
  62.  
  63. /**Class for asynchronous tasks**/
  64. public class DownloadAsync extends AsyncTask<AsyncParameters, Void, Void> {
  65.  
  66. // Container for all parameters of DownloadAsync
  67. ProgressDialog pDialog;
  68.  
  69. @Override
  70. protected void onPreExecute() {
  71. super.onPreExecute();
  72. pDialog = new ProgressDialog(pdfView.this);
  73. pDialog.setMessage("Downloading Database...");
  74. String message= "Downloading Files";
  75.  
  76. SpannableString ss2 = new SpannableString(message);
  77. ss2.setSpan(new RelativeSizeSpan(2f), 0, ss2.length(), 0);
  78. ss2.setSpan(new ForegroundColorSpan(Color.BLACK), 0, ss2.length(), 0);
  79.  
  80. pDialog.setMessage(ss2);
  81. pDialog.setCancelable(false);
  82. pDialog.show();
  83. }
  84.  
  85. @Override
  86. protected Void doInBackground(AsyncParameters... params) {
  87. Log.d("WE ARE IN DOBACKGROUND", "doInBackground: ");
  88. String fileURL = params[0].URL;
  89. File directory = params[0].directory;
  90. try {
  91.  
  92. FileOutputStream f = new FileOutputStream(directory);
  93. java.net.URL u = new URL(fileURL);
  94. HttpURLConnection c = (HttpURLConnection) u.openConnection();
  95. c.connect();
  96. InputStream in = c.getInputStream();
  97.  
  98. byte[] buffer = new byte[8192];
  99. int len1 = 0;
  100. while ((len1 = in.read(buffer)) > 0) {
  101. f.write(buffer, 0, len1);
  102. }
  103. f.close();
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. onPostExecute();
  108. return null;
  109. }
  110.  
  111. protected void onPostExecute() {
  112. pDialog.dismiss();
  113. }
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment