-1

I am developing an application to download pdf file in sd card with the url entered in edittext , after clicking the submit button.

       Button b = (Button)findViewById(R.id.button1);
       final EditText ed = (EditText)findViewById(R.id.editText1);
       b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         String URL  = ed.getText().toString();
          WebView web1 =(WebView)findViewById(R.id.webview);
          web1.getSettings().setJavaScriptEnabled(true);
           web1.loadUrl(URL);  
           }
          });
3
  • 3
    what you have tried so far, post you logic code or where you getting stuck? then only someone help you well
    – swiftBoy
    Jun 6, 2012 at 11:37
  • please give more information and tell me what you have done? Jun 6, 2012 at 11:38
  • please see my answer for download pdf file. Jun 6, 2012 at 11:45

2 Answers 2

1

Below code may help you -

public class DownloaderThread extends Thread
{
        // constants
        private static final int DOWNLOAD_BUFFER_SIZE = 4096;

        // instance variables
        private AndroidFileDownloader parentActivity;
        private String downloadUrl;

        /**
         * Instantiates a new DownloaderThread object.
         * @param parentActivity Reference to AndroidFileDownloader activity.
         * @param inUrl String representing the URL of the file to be downloaded.
         */
        public DownloaderThread(AndroidFileDownloader inParentActivity, String inUrl)
        {
                downloadUrl = "";
                if(inUrl != null)
                {
                        downloadUrl = inUrl;
                }
                parentActivity = inParentActivity;
        }

        /**
         * Connects to the URL of the file, begins the download, and notifies the
         * AndroidFileDownloader activity of changes in state. Writes the file to
         * the root of the SD card.
         */
        @Override
        public void run()
        {
                URL url;
                URLConnection conn;
                int fileSize, lastSlash;
                String fileName;
                BufferedInputStream inStream;
                BufferedOutputStream outStream;
                File outFile;
                FileOutputStream fileStream;
                Message msg;

                // we're going to connect now
                msg = Message.obtain(parentActivity.activityHandler,
                                AndroidFileDownloader.MESSAGE_CONNECTING_STARTED,
                                0, 0, downloadUrl);
                parentActivity.activityHandler.sendMessage(msg);

                try
                {
                        url = new URL(downloadUrl);
                        conn = url.openConnection();
                        conn.setUseCaches(false);
                        fileSize = conn.getContentLength();

                        // get the filename
                        lastSlash = url.toString().lastIndexOf('/');
                        fileName = "file.bin";
                        if(lastSlash >=0)
                        {
                                fileName = url.toString().substring(lastSlash + 1);
                        }
                        if(fileName.equals(""))
                        {
                                fileName = "file.bin";
                        }

                        // notify download start
                        int fileSizeInKB = fileSize / 1024;
                        msg = Message.obtain(parentActivity.activityHandler,
                                        AndroidFileDownloader.MESSAGE_DOWNLOAD_STARTED,
                                        fileSizeInKB, 0, fileName);
                        parentActivity.activityHandler.sendMessage(msg);

                        // start download
                        inStream = new BufferedInputStream(conn.getInputStream());
                        outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
                        fileStream = new FileOutputStream(outFile);
                        outStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
                        byte[] data = new byte[DOWNLOAD_BUFFER_SIZE];
                        int bytesRead = 0, totalRead = 0;
                        while(!isInterrupted() && (bytesRead = inStream.read(data, 0, data.length)) >= 0)
                        {
                                outStream.write(data, 0, bytesRead);

                                // update progress bar
                                totalRead += bytesRead;
                                int totalReadInKB = totalRead / 1024;
                                msg = Message.obtain(parentActivity.activityHandler,
                                                AndroidFileDownloader.MESSAGE_UPDATE_PROGRESS_BAR,
                                                totalReadInKB, 0);
                                parentActivity.activityHandler.sendMessage(msg);
                        }

                        outStream.close();
                        fileStream.close();
                        inStream.close();

                        if(isInterrupted())
                        {
                                // the download was canceled, so let's delete the partially downloaded file
                                outFile.delete();
                        }
                        else
                        {
                                // notify completion
                                msg = Message.obtain(parentActivity.activityHandler,
                                                AndroidFileDownloader.MESSAGE_DOWNLOAD_COMPLETE);
                                parentActivity.activityHandler.sendMessage(msg);
                        }
                }
                catch(MalformedURLException e)
                {
                        String errMsg = parentActivity.getString(R.string.error_message_bad_url);
                        msg = Message.obtain(parentActivity.activityHandler,
                                        AndroidFileDownloader.MESSAGE_ENCOUNTERED_ERROR,
                                        0, 0, errMsg);
                        parentActivity.activityHandler.sendMessage(msg);
                }
                catch(FileNotFoundException e)
                {
                        String errMsg = parentActivity.getString(R.string.error_message_file_not_found);
                        msg = Message.obtain(parentActivity.activityHandler,
                                        AndroidFileDownloader.MESSAGE_ENCOUNTERED_ERROR,
                                        0, 0, errMsg);
                        parentActivity.activityHandler.sendMessage(msg); 
                }
                catch(Exception e)
                {
                        String errMsg = parentActivity.getString(R.string.error_message_general);
                        msg = Message.obtain(parentActivity.activityHandler,
                                        AndroidFileDownloader.MESSAGE_ENCOUNTERED_ERROR,
                                        0, 0, errMsg);
                        parentActivity.activityHandler.sendMessage(msg); 
                }
        }

}

Above code extracted from this Existing project. Don't forget to give the required permission for Internet

1

Here urlToDownload is your edittext's text, Write Below Method into your button click event to Download pdf file.

public void downloadPdfContent(String urlToDownload){
     try {
          String fileName="xyz";
          String fileExtension=".pdf";
          //download pdf file.
          URL url = new URL(urlToDownload);
          HttpURLConnection c = (HttpURLConnection) url.openConnection();
          c.setRequestMethod("GET");
          c.setDoOutput(true);
          c.connect();
          String PATH = Environment.getExternalStorageDirectory() + "/mydownload/";
          File file = new File(PATH);
          file.mkdirs();
          File outputFile = new File(file, fileName+fileExtension);
          FileOutputStream fos = new FileOutputStream(outputFile);
          InputStream is = c.getInputStream();
          byte[] buffer = new byte[1024];
          int len1 = 0;
          while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
          }
          fos.close();
          is.close();
          System.out.println("--pdf downloaded--ok--"+urlToDownload);
     } catch (Exception e) {
          e.printStackTrace();
     }
}

and give write_external_storage permission into your application manifest file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.