0

I am trying to download a pdf file from my app.

I've tried two methods:

webView.loadUrl(uriPath);
webView.setDownloadListener(new DownloadListener()
{
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) 
    {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }
});

and next

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uriPath));
startActivity(i);

Both of these methods are using Intent.ACTION_VIEW

End of the day my file is downloaded but before downloading it is showing the web browser, loading the page, then coming back to my app, giving a Toast Downloading

I want to hide the browser, there are questions to hide the url on browser but not to hide the browser.

I found an unanswered question Here

Kindly suggest me how to do this task. Thank you.

Edit: Structure of my activity

public class Pdf extends Activity
{
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
     new Displays().execute();
  }

  private class Displays extends AsyncTask<String, Void, Void>
  {
    @Override
    protected Void doInBackground(String... params)
    {
       //retrieving the list of pdf documents url.
    }

    @Override
    protected void onPostExecute(Void aVoid)
    {
       text = new TextView(getApplicationContext());
       text.setText(list1.get(i));
       text.setOnClickListener(...)
       {
         //here download the selected pdf
//if am am calling any async class or any method from here file is not getting downloaded.What to do :(
       }
    }
  }
}
Prabs
  • 4,923
  • 6
  • 38
  • 59
  • 3
    If you don't want to open the url but only dowload the content, i think, you should better just implement a background task that download the file. – sonic Jul 31 '15 at 11:07
  • Great idea @sonic I'm trying to implement it.could you provide any link please – Prabs Jul 31 '15 at 12:36
  • @sonic I have a list of pdf files(textview generated programmatically), onClick on a particular view i have to download that file,now onclick we have to do in async onpost, but we can't download in onpost,we have to do in background..What should I do now – Prabs Jul 31 '15 at 13:02
  • Create an async task (or an intent service). Start it with the url to download as params. You can now download your file as you are in a background task. You can find tutorial on async task or intent service to help you start. – sonic Jul 31 '15 at 13:11
  • the control is in onPost when i get the list of options..if am calling another async class in onpost it is not executing – Prabs Jul 31 '15 at 14:01
  • @sonic Task completed.I've called the Async class #2 from onPost and defined the #2 class outside of #1.Given progress dialogs and alerts to control the downloading.Now it is downloaded within app :) – Prabs Aug 01 '15 at 05:34

0 Answers0