android - Open pdf file url which is stored in Firebase storage -


i have uploaded pdf file firebase storage, after uploading pdf file firebase storage, getting download url. want open pdf file download url in application.

below url getting after uploading pdf file firebase storage.

https://firebasestorage.googleapis.com/v0/b/realtime-chat-46f4c.appspot.com/o/documents%2fbf307aa5-79ae-4532-8128-ee394537b357.pdf?alt=media&token=2d0c5329-4717-4adc-9418-6614913e5bfa

now want open intent view pdf file, i've used below code :

string url = "https://firebasestorage.googleapis.com/v0/b/realtime-chat-46f4c.appspot.com/o/documents%2fbf307aa5-79ae-4532-8128-ee394537b357.pdf?alt=media&token=2d0c5329-4717-4adc-9418-6614913e5bfa";  intent intent = new intent(android.content.intent.action_view); intent.setdataandtype(uri.parse(url), "application/pdf"); startactivity(intent.createchooser(intent, "choose application:")); 

my phone having applications can open pdf file, still says there no applications installed view file.

if convert url in file , use below code, chooser of applications opened gives error file can't opened.

intent intent = new intent(android.content.intent.action_view); file file = new file(name); intent.setdataandtype(uri.fromfile(file), "aplication/pdf"); 

i've seen many answers says first download file , open it, don't want download file, want view it.

please me if have idea this.

thanks lot in advanced.

please add following files/method project

  1. pdfdownloader.java
  2. pdfdownloaderasynctask.java

and using following method handleviewpdf view pdf:

private void handleviewpdf () {      file folder = getappdirectory(context);     string filename = "test.pdf";// getpdffilename(pdfurl);     file pdffile = new file(folder, filename);      if (pdffile.exists () && pdffile.length () > 0) {         openpdffile (context, uri.fromfile(pdffile));     }     else {         if (pdffile.length () == 0) {             pdffile.delete ();         }         try {             pdffile.createnewfile ();         }         catch (ioexception e) {             e.printstacktrace ();         }         arraylist<string> filenameandurl = new arraylist<> ();         filenameandurl.add (pdffile.tostring ());         filenameandurl.add (pdfurl);         filenameandurl.add (filename);         if (pdfdownloaderasynctask == null) {             pdfdownloaderasynctask = new pdfdownloaderasynctask (context, pdffile);         }         if (hasinternetconnection (context)) {             if (!pdfdownloaderasynctask.isdownloadingpdf ()) {                 pdfdownloaderasynctask = new pdfdownloaderasynctask (context, pdffile);                 pdfdownloaderasynctask.execute (filenameandurl);             }         }         else {             //show error         }     } } 

pdfdownloaderasynctask.java

    import java.io.file;     import java.util.arraylist;      import android.content.context;     import android.net.uri;     import android.os.asynctask;     import android.os.handler;     import android.text.textutils;     import android.widget.toast;       public class pdfdownloaderasynctask extends asynctask<arraylist<string>, void, string> {          private boolean isdownloadingpdf = false;          private file    file;         private context context;          public pdfdownloaderasynctask (context context, file file) {              this.file = file;             this.context = context;             this.isdownloadingpdf = false;         }          public boolean isdownloadingpdf () {              return this.isdownloadingpdf;         }          @override         protected void onpreexecute () {              super.onpreexecute ();             //show loader etc         }          @override         protected string doinbackground (arraylist<string>... params) {              isdownloadingpdf = true;             file file = new file (params[0].get (0));             string filestatus = pdfdownloader.downloadfile (params[0].get (1), file);             return filestatus;         }          @override         protected void onpostexecute (string result) {              super.onpostexecute (result);             loader.hideloader ();             if (!textutils.isempty (result) && result.equalsignorecase (context.getstring (r.string.txt_success))) {                 showpdf ();             }             else {                 isdownloadingpdf = false;                 toast.maketext (context, context.getstring (r.string.error_could_not_download_pdf), toast.length_long).show ();                 file.delete ();             }         }          @override         protected void oncancelled () {              isdownloadingpdf = false;             super.oncancelled ();             //loader.hideloader ();         }          @override         protected void oncancelled (string s) {              isdownloadingpdf = false;             super.oncancelled (s);             //loader.hideloader ();         }          private void showpdf () {              new handler ().postdelayed (new runnable () {                 @override                 public void run () {                      isdownloadingpdf = false;                     openpdffile (context, uri.fromfile (file));                 }             }, 1000);         }     } 

pdfdownloader.java

package com.pdf;  import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url;  public class pdfdownloader {     private static final int megabyte = 1024 * 1024;      public static string downloadfile (string fileurl, file directory) {          string downloadstatus;         try {              url url = new url (fileurl);             httpurlconnection urlconnection = (httpurlconnection) url.openconnection ();             urlconnection.connect ();              inputstream inputstream = urlconnection.getinputstream ();             fileoutputstream fileoutputstream = new fileoutputstream (directory);             int totalsize = urlconnection.getcontentlength ();              log.d ("pdf", "total size: " + totalsize);             byte[] buffer = new byte[megabyte];             int bufferlength = 0;             while ((bufferlength = inputstream.read (buffer)) > 0) {                 fileoutputstream.write (buffer, 0, bufferlength);             }             downloadstatus = "success";             fileoutputstream.close ();         }         catch (filenotfoundexception e) {             downloadstatus = "filenotfoundexception";             e.printstacktrace ();         }         catch (malformedurlexception e) {             downloadstatus = "malformedurlexception";             e.printstacktrace ();         }         catch (ioexception e) {             downloadstatus = "ioexception";             e.printstacktrace ();         }         log.d ("pdf", "download status: " + downloadstatus);         return downloadstatus;     }       public static void openpdffile (context context, uri path) {          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, context.getstring (r.string.txt_no_pdf_available), toast.length_short).show ();         }         loader.hideloader ();     }      public static file getappdirectory (context context) {          string extstoragedirectory = environment.getexternalstoragedirectory ().tostring ();         file folder = new file (extstoragedirectory, context.getstring (r.string.app_folder_name).trim ());         if (!folder.exists ()) {             boolean success = folder.mkdirs();             log.d ("directory", "mkdirs():" + success);         }         return folder;     }  } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -