Question

I am working on a project and in that I am downloading a .pdf file when clicked on ListView and showing horizontal progress bar at the same time.. But, it stops at 1% and is not moving forward and also the file is not getting downloaded..

Please check the code.. what I am missing??

CODE

public class Downloads_screen extends Activity
{

ProgressDialog mProgressDialog;

String mPdf_links;
String mlist;
String img_path;

@Override
protected void onCreate(Bundle savedInstanceState)
    {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.downloads_screen);

    Intent i = getIntent();
    mlist = i.getExtras().getString("TITLE");
    mPdf_links = i.getExtras().getString("PDFPATH");
    img_path = i.getExtras().getString("IMGPATH");

    Log.v("Download Page", "Title: "+mlist);
    Log.v("Download Page", "Pdf Path: "+mPdf_links);

    RelativeLayout backbutton = (RelativeLayout) findViewById(R.id.download_screen_backbutton);

    backbutton.setOnClickListener(new OnClickListener()
              {
        @Override
        public void onClick(View v)
               {
            // TODO Auto-generated method stub
            finish();
        }
    });

    Boolean isFilePresent;

          String filename = mPdf_links.substring(mPdf_links.lastIndexOf('/') + 1,                mPdf_links.length());
        File downloadfile = new File("/sdcard/CONSTRUCTION/" + filename);
        if (downloadfile.exists()) {
            isFilePresent = true;
        } else {
            isFilePresent= false;
        }
        System.out.println(filename + ":" + isFilePresent);


    MyDownload_adapter adapter = new MyDownload_adapter(Downloads_screen.this, mlist, img_path, isFilePresent);
    ListView downloadlist = (ListView) findViewById(R.id.download_screen_listView1);
    downloadlist.setAdapter(adapter);
    downloadlist.setOnItemClickListener(new OnItemClickListener() {

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            // instantiate it within the onCreate method
            String filename = mPdf_links.substring(
                    mPdf_links.lastIndexOf('/') + 1,
                    mPdf_links.length());
            System.out.println("FileName:" + filename);
            File downloadfile = new File("/sdcard/CONSTRUCTION/" + filename);
            if (!downloadfile.exists()) {
                mProgressDialog = new ProgressDialog(Downloads_screen.this);
                mProgressDialog.setMessage("Downloading..");
                mProgressDialog.setIndeterminate(false);
                mProgressDialog.setProgress(0);
                mProgressDialog.incrementProgressBy(1);
                mProgressDialog.setMax(100);
                mProgressDialog
                        .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                // execute this when the downloader must be fired
                DownloadFile downloadFile = new DownloadFile();
                System.out.println(mPdf_links);
                downloadFile.execute(mPdf_links);

            } else {
                File file = new File(Environment
                        .getExternalStorageDirectory().getAbsolutePath()
                        + "/CONSTRUCTION/" + filename);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);

            }
        }
    });

}

private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100%
            // progress bar
            int fileLength = connection.getContentLength();
            System.out.println("File length:" + fileLength);
            System.out.println("TEST here in Downloadfile");
            // download the file
            File wallpaperDirectory = new File("/sdcard/CONSTRUCTION/");
            // have the object build the directory structure, if needed.
            wallpaperDirectory.mkdirs();
            // create a File object for the output file

            String filename = sUrl[0].substring(
                    sUrl[0].lastIndexOf('/') + 1, sUrl[0].length());
            System.out.println("FileName:" + filename);
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(
                    "/sdcard/CONSTRUCTION/" + filename);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                System.out.println("Total downloaded" + total + " count:"
                        + count);
                // publishing the progress....
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            mProgressDialog.dismiss();
            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/CONSTRUCTION/" + filename);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);

        } catch (Exception e) {
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }
}
}
Was it helpful?

Solution

My above code is working perfect alright..

Just the problem was the name of the file i was getting is not proper i.e. it has special characters and blank spaces too which the downloader couldn't get it so the downloading wasn't initiated.

Just tell your web_dept person to rename file to proper format. or you have to use the string operations to rename the file properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top