Monday, May 20, 2013

[android-developers] Re: Donwloadin incomplete file

I'd say you are probably trying to write to a bad filename.

Maybe try changing the file open code to this:

FileOutputStream fos = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/"
+ "test.hmm");



Thanks.


On Monday, May 20, 2013 7:25:12 AM UTC-5, rahul kaushik wrote:
Hi,

I am trying to download the file as this is 2 mb but it is downlaoding only 7.1 kb below is my code
class DownloadFileFromURL extends AsyncTask<String, String, String> {

   /**
    * Before starting background thread Show Progress Bar Dialog
    * */
   @Override
   protected void onPreExecute() {
       super.onPreExecute();
       showDialog(progress_bar_type);
   }

   /**
    * Downloading file in background thread
    * */
   @Override
   protected String doInBackground(String... f_url) {
       int count;
       final int BUFFER_SIZE = 128;
       File root = android.os.Environment.getExternalStorageDirectory();
       try {
           URL url = new URL(f_url[0]);
           String fileName=url.toString();;
// String Filename=   getFileName(url);
         
           String newpath = fileName.replace('\\','/');
           int start = newpath.lastIndexOf("=");
           if ( start == -1)
           {
             start = 0;
           }
           else
           {
             start = start + 1;
           }
           String pageName = newpath.substring(start, newpath.length());
  
        
           long startTime = System.currentTimeMillis();
        Log.d("getFileName", pageName);
      
        
        URLConnection ucon = url.openConnection();
        ucon.connect();
        int lengthofFile = ucon.getContentLength();
             /*
              * Define InputStreams to read from the URLConnection.
              */
             InputStream is = ucon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

             /*
              * Read bytes to the Buffer until there is nothing more to read(-1).
              */
             ByteArrayBuffer baf = new ByteArrayBuffer(50000);
             int current = 0;
             while ((current = bis.read()) != -1) {
                     baf.append((byte) current);
             }

             /* Convert the Bytes read to a String. */
             File file = new File(fileName);
             FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+ "/"+pageName.trim());
             //Log.d("root:", root.toString());
             fos.write(baf.toByteArray());
             fos.close();
             Log.d("ImageManager", "download ready in"
                             + ((System.currentTimeMillis() - startTime) / 1000)
                             + " sec");
        
        
        
        
        
       } catch (Exception e) {
           Log.e("Error: ", e.getMessage());
       }

       return null;
   }

   /**
    * Updating progress bar
    * */
   protected void onProgressUpdate(String... progress) {
       // setting progress percentage
       pDialog.setProgress(Integer.parseInt(progress[0]));
   }

   /**
    * After completing background task Dismiss the progress dialog
    * **/
   @Override
   protected void onPostExecute(String file_url) {
       // dismiss the dialog after the file was downloaded
       dismissDialog(progress_bar_type);
      // String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
            // setting downloaded into image view
          //  my_image.setImageDrawable(Drawable.createFromPath(imagePath));
   }

}

PLease Suggest
Thanks
RK

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment