Download Zip to SD card, zip not getting read

Discussion in 'Application Development' started by cul, Jan 13, 2011.

  1. cul

    cul Member
    15

    Jan 13, 2011
    5
    0
    15
    hello there, tried following code to download zip to sd card, i get NullpointerException. when tried some interrogations i come to know that the zip file is not getting downloaded actually. So would you please help whether code needs some changes or anything wrong with zip? M stuck on that point only. Please help... my code as follows... help appreciated... :thinking:

    Code (Text):
    1.   {
    2.         super.onCreate(savedInstanceState);
    3.         setContentView(R.layout.main);
    4.         String from = "http://192.168.1.63/ZipFile/Text.zip" ;
    5.         String to = Environment.getExternalStorageDirectory() + "/newunzip/";
    6.        
    7.         try {
    8.             ((TextView) findViewById(R.id.display)).append("\n in try after function :");
    9.             downloadFile(from, to);
    10.            
    11.         } catch (Exception e) {
    12.             // TODO Auto-generated catch block
    13.             e.printStackTrace();
    14.             ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :");
    15.             ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage());
    16.             ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString());
    17.         }
    18.        
    19.     }
    20.    
    21.     private void downloadFile(String from, String to) throws Exception
    22.     {
    23.         ((TextView) findViewById(R.id.display)).append("\n \n in function call :");
    24.         HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
    25.         conn.setDoInput(true);
    26.         conn.setConnectTimeout(100000); // timeout 100 secs
    27.         conn.connect();
    28.        
    29.         ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn);
    30.         InputStream input = conn.getInputStream();
    31.        
    32.         byte[] b = null;
    33.         input.read(b);
    34.         ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);
    35.        
    36.         FileOutputStream fOut = new FileOutputStream(to);
    37.        
    38.         byte[] b1 = null;
    39.         input.read(b1);
    40.         ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1);
    41.        
    42.         int byteCount = 0;
    43.         byte[] buffer = new byte[4096];
    44.         int bytesRead = -1;
    45.         while ((bytesRead = input.read(buffer)) != -1)
    46.         {
    47.             ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :");
    48.             fOut.write(buffer, 0, bytesRead);
    49.             byteCount += bytesRead;
    50.         }
    51.         fOut.flush();
    52.         ((TextView) findViewById(R.id.display)).append("\n \n flush & close :");
    53.         fOut.close();
    54.     }
    55.  
    56. }
     

    Advertisement
  2. cul

    cul Member
    15

    Jan 13, 2011
    5
    0
    15
    answer plzzzzzzzz..........
     
  3. miXer

    miXer Well-Known Member
    53

    Aug 12, 2010
    313
    51
    53
    Norway
    Post the excpetion your getting in logcat here. This will make it much easier for us to help you.
     
  4. cul

    cul Member
    15

    Jan 13, 2011
    5
    0
    15
    it givin null pointer exception
     
  5. DylanTimby

    DylanTimby Active Member
    26

    Jan 3, 2011
    36
    5
    26
    Freelancer
    Sunderland, United Kingdom
    Hello there, due to the point at Android OS is based on Linux .zip files are not supported, but I am sure .rar is :), Feel free to PM me if you need anymore help.




    Thanks,
    Dylan
     
  6. miXer

    miXer Well-Known Member
    53

    Aug 12, 2010
    313
    51
    53
    Norway
    Yes, i know that. But the full exception includes which line number and stuff the exception occured on.
     
  7. miXer

    miXer Well-Known Member
    53

    Aug 12, 2010
    313
    51
    53
    Norway
    Android has this package included from java. That would mean android should support it?

    java.util.zip | Android Developers
     
  8. cul

    cul Member
    15

    Jan 13, 2011
    5
    0
    15
    OK thanx for your reply guys! first of all .zip is not getting downloaded which i resolved with using "astro" in which I've given permission to download all files.

    But after that it's not going to download that file instead it giving null pointer exception... n no other message it's generating. only null pointer exception, m stuck here badly.. :( plz help me out!
     

Share This Page

Loading...