0

I need to download the pdf from below URL in Android. Any idea how this can be done:

http://bkinfo.in/Murli/1305/EME-26-05-2013.pdf

Similarly, there is an mp3: http://bkinfo.in/Murli/1305/26-05-2013.mp3

Appreciate the ideas..

Finally.... Here is the full code that I used. May be useful for someone.. Add these to manifest:

Make sure your AVD can write to SDCard(if you are writing to card). U can set it by assigning memory chunck to SDCard in AVD Manager.

    public class MainActivity extends Activity {
            static ProgressDialog pd;
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    pd = new ProgressDialog(this);
                    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    pd.setCancelable(false);
                    AsyncTaskTest at = new AsyncTaskTest();
                    at.execute();
            }

            public class AsyncTaskTest extends AsyncTask<Void, Integer, Integer> {
                    Session s = null;
                    protected void onPreExecute(){
                            pd.show();
                    }

                    protected Integer doInBackground(Void... vd){
                            try{
                                    String[] urls = new String[3];
                                    urls[0] = "http://bkinfo.in/Murli/1305/HMS-25-05-2013.pdf";
                                    urls[1] = "http://bkinfo.in/Murli/1305/EME-25-05-2013.pdf";
                                    urls[2] = "http://bkinfo.in/Murli/1305/25-05-2013.mp3";
                                    String fileName = urls[2].substring(urls[2].lastIndexOf("/")+1); //Coupying the mp3
                                    URL url = new URL(urls[2]);
                                    URLConnection conection = url.openConnection();
                                    conection.setConnectTimeout(10000);
                                    conection.connect();
                                    int lenghtOfFile = conection.getContentLength();
                                    InputStream input = new BufferedInputStream(url.openStream(),8192);
                                    OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+fileName);
                                    byte data[] = new byte[1024];
                                    long total = 0;
                                    while ((count = input.read(data)) != -1){
                                            total += count;
                                            output.write(data, 0, count);
                                            publishProgress((int) ((total * 100) / lenghtOfFile));
                                    }
                                    output.flush();
                                    output.close();
                                    input.close();
                            }catch(Exception e){
                                    Log.e("MyError:",e.toString());
                            }
                            return 0;
                    }

                    protected void onProgressUpdate(Integer... msg) {
                            pd.setProgress(msg[0]);
                    }

                    protected void onPostExecute(Integer in){
                            pd.dismiss();
                            showDialog("Done !");
                    }

                    private void showDialog(String msg){
                    final AlertDialog.Builder alertBox = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, android.R.style.Theme_Dialog));
                    alertBox.setMessage(msg);
                    alertBox.setCancelable(false)
                                    .setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialog,int id){
                                            dialog.cancel();
                                    }
                            }).show();
                    }
            }
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
ravi tiwari
  • 521
  • 1
  • 8
  • 24
  • might be relevant: http://stackoverflow.com/questions/4457492/simple-http-example-in-android – mvp May 27 '13 at 22:53

1 Answers1

0

Hope I am not spoon feeding! I have some tough time so I decided to share my working code.

private void downloadCommandFile(String dlUrl){
    int count;
    try {
        URL url = new URL( dlUrl );
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.connect();
        int fileSize = con.getContentLength();
        Log.d("TAG", "Download file size = " + fileSize );
        InputStream is = url.openStream();
        String dir = Environment.getExternalStorageDirectory() + "dl_directory";
        File file = new File( dir );
        if( !file.exists() ){
            file.mkdir();
        }

        FileOutputStream fos = new FileOutputStream(file + "EME-26-05-2013.pdf");
        byte data[] = new byte[1024];

        while( (count = is.read(data)) != -1 ){
            fos.write(data, 0, count);
        }

        is.close();
        fos.close();


    } catch (Exception e) {
        Log.e("TAG", "DOWNLOAD ERROR = " + e.toString() );
    }

}




public class DownloadTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
             String url = "http://bkinfo.in/Murli/1305/EME-26-05-2013.pdf"; // your url here
        downloadCommandFile( url);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // download complete
    }

}

Call Async Task as follows:

new DownloadTask().execute();

And don't forget to add this to your manifest file:

<uses-permission android:name="android.permission.INTERNET"/>
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103