Results 1 to 8 of 8
  1. #1
    Laurenz is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Http Download to file on sdcard

    Hi everybody,
    I have a problem with a code snippet which should download a file onto the sdcard.
    Java Code:
    Sync("http://themen.co.nu/Dateiliste/",Environment.getExternalStorageDirectory().getAbsolutePath() + "/Quiz/Dateiliste");
    That's the method:
    Java Code:
    public void Sync(String URLS, String DateiName){
    		try {
    	       syn = true;
    	        URL url = new URL(URLS);
    
    	        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    
    	        urlConnection.setRequestMethod("GET");
    	        urlConnection.setDoOutput(true);
    
    	        //and connect!
    	        urlConnection.connect();
    
    	        //set the path where we want to save the file
    	        //in this case, going to save it on the root directory of the
    	        //sd card.
    	        
    	        //create a new file, specifying the path, and the filename
    	        //which we want to save the file as.
    	        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Quiz/" + DateiName);
    
    	        //this will be used to write the downloaded data into the file we created
    	        FileOutputStream fileOutput = new FileOutputStream(file);
    
    	        //this will be used in reading the data from the internet
    	        InputStream inputStream = urlConnection.getInputStream();
    	        
    	        //this is the total size of the file
    	        int totalSize = urlConnection.getContentLength();
    	        //variable to store total downloaded bytes
    	        int downloadedSize = 0;
    
    	        //create a buffer...
    	        byte[] buffer = new byte[1024];
    	        int bufferLength = 0; //used to store a temporary size of the buffer
    
    	        //now, read through the input buffer and write the contents to the file
    	        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
    	                //add the data in the buffer to the file in the file output stream (the file on the sd card
    	                fileOutput.write(buffer, 0, bufferLength);
    	                //add up the size so we know how much is downloaded
    	                downloadedSize += bufferLength;
    	                //this is where you would do something to report the prgress, like this maybe
    	                ProgB.setMax(totalSize);
    	                ProgB.incrementProgressBy(downloadedSize);
    	              
    
    	        }
    	        //close the output stream when done
    	        fileOutput.close();
    	        syn = false;
    	        Alert("Fertig","syn beendet");
    	//catch some possible errors...
    	        /*
    	 * */
    	
    	} catch (MalformedURLException e) {
    	        e.printStackTrace();
    	        Alert("F1",e.getMessage());
    	} catch (IOException e) {
    	        e.printStackTrace();
    	        Alert("F2",e.getMessage());
    	        
    	}
    	
    	}
    It even can't show the exception messages. I got a black screen with no reaktions on my input anymore. It shows a window with F1 as title but no text. I also set the permissions Internet and read phone state
    I hope you can help me,
    Laurenz

  2. #2
    Laurenz is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Http Download to file on sdcard

    Ok, I am wrong. It's still not working. There is no error anymore, but the file is empty. I don't know what to do now. I hope you can help me.
    Java Code:
    File Ordnerst = new File("/sdcard/Quiz/Themen");
    			Ordnerst.mkdirs();
    				
    		
    			try {
    				File file = new File(DateiName);
    				 if (file.exists()){
    			        	Alert("D","E");
    			        	
    			        } else {
    			        	file.createNewFile();
    			        	Alert("D","C");
    			        }
    			
    	       syn = true;
    	        URL url = new URL(URLS);
    
    	        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    
    	        urlConnection.setRequestMethod("GET");
    	        urlConnection.setDoOutput(true);
    	        urlConnection.setDoInput(true);
    	        
    	        InputStream inputStream = urlConnection.getInputStream();
    	      
    	        urlConnection.connect();
    	       
    	        
    	        
    	        FileOutputStream fileOutput = new FileOutputStream(file);
    	      
    				
    
    	        int totalSize = urlConnection.getContentLength();
                //variable to store total downloaded bytes
                int downloadedSize = 0;
     
                //create a buffer...
                byte[] buffer = new byte[1024];
                int bufferLength = 0; //used to store a temporary size of the buffer
     
                //now, read through the input buffer and write the contents to the file
                while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                        //add the data in the buffer to the file in the file output stream (the file on the sd card
                        fileOutput.write(buffer, 0, bufferLength);
                        //add up the size so we know how much is downloaded
                        downloadedSize += bufferLength;
                        //this is where you would do something to report the prgress, like this maybe
                        ProgB.setMax(totalSize);
                        ProgB.incrementProgressBy(downloadedSize);
                       
     
                }
                //close the output stream 
    			
    		
    	        //close the output stream when done
    	        fileOutput.close();
    	        syn = false;
    	        
    	        Alert("Fertig","syn beendet");
    	//catch some possible errors...
    	  
    	
    	} catch (MalformedURLException e) {
    	        e.printStackTrace();
    	        Alert("F1",e.getMessage());
    	} catch (IOException e) {
    	        e.printStackTrace();
    	        Alert("F2",e.getMessage());
    	        
    	}

  3. #3
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Http Download to file on sdcard

    Try this code:
    I hope this will be help to you
    //create the new connection
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    //set up some things on the connection
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);

    //and connect!
    urlConnection.connect();

    //set the path where we want to save the file
    //in this case, going to save it on the root directory of the
    //sd card.
    File SDCardRoot = Environment.getExternalStorageDirectory();
    //create a new file, specifying the path, and the filename
    //which we want to save the file as.
    File file = new File(SDCardRoot,"file.ext");

    //this will be used to write the downloaded data into the file we created
    FileOutputStream fileOutput = new FileOutputStream(file);

    //this will be used in reading the data from the internet
    InputStream inputStream = urlConnection.getInputStream();

    //this is the total size of the file
    int totalSize = urlConnection.getContentLength();
    //variable to store total downloaded bytes
    int downloadedSize = 0;

    //create a buffer...
    byte[] buffer = new byte[1024];
    int bufferLength = 0; //used to store a temporary size of the buffer

    //now, read through the input buffer and write the contents to the file
    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
    //add the data in the buffer to the file in the file output stream (the file on the sd card
    fileOutput.write(buffer, 0, bufferLength);
    //add up the size so we know how much is downloaded
    downloadedSize += bufferLength;

    }
    //close the output stream when done
    fileOutput.close();

    //catch some possible errors...
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    Last edited by tamilarasi; 02-19-2013 at 10:50 AM.
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  4. #4
    Laurenz is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Http Download to file on sdcard

    Thanks for the answer but I took this code and modified it a bit to optimise it for my project and I would like to know why the script isn't working.
    Laurenz

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Http Download to file on sdcard

    Can you please explain clearly when you met a struggle in your code?
    That is easy to help you....
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Http Download to file on sdcard

    Java Code:
    while ( (bufferLength = inputStream.read(buffer)) > 0 )
    The end of an input stream is represented by a -1 coming back from that read().
    0 is a valid response meaning "no data read yet", not "eos".

    That may not be your exact problem, but it is a problem.

    tamilarasi - please use [code] tags [/code] when posting code, not [quote] tags [/quote].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Laurenz is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Http Download to file on sdcard

    I can't tell you where my code is struggling cause I got no errormessage. The only thing which is not working is, that the file is empty.
    Laurenz

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Http Download to file on sdcard

    Have you fixed your while loop?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. IOException while writing a file to sdcard
    By achill in forum Android
    Replies: 2
    Last Post: 05-11-2011, 10:08 PM
  2. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  3. Download file from URL?
    By makpandian in forum Networking
    Replies: 0
    Last Post: 03-10-2009, 12:31 PM
  4. Download file from URL?
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 12:30 PM
  5. How to download an image from a HTTP URL
    By Valeriano in forum Networking
    Replies: 1
    Last Post: 05-20-2007, 08:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •