• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Download Zip to SD card, zip not getting read

cul

Lurker
Jan 13, 2011
5
0
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:
  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String from = "http://192.168.1.63/ZipFile/Text.zip" ;
        String to = Environment.getExternalStorageDirectory() + "/newunzip/";
        
        try {
            ((TextView) findViewById(R.id.display)).append("\n in try after function :");
            downloadFile(from, to);
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :");
            ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage());
            ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString());
        }
        
    }
    
    private void downloadFile(String from, String to) throws Exception 
    {
        ((TextView) findViewById(R.id.display)).append("\n \n in function call :");
        HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
        conn.setDoInput(true);
        conn.setConnectTimeout(100000); // timeout 100 secs
        conn.connect();
        
        ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn);
        InputStream input = conn.getInputStream();
        
        byte[] b = null;
        input.read(b);
        ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);
        
        FileOutputStream fOut = new FileOutputStream(to);
        
        byte[] b1 = null;
        input.read(b1);
        ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1);
        
        int byteCount = 0;
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = input.read(buffer)) != -1) 
        {
            ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :");
            fOut.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        fOut.flush();
        ((TextView) findViewById(R.id.display)).append("\n \n flush & close :");
        fOut.close();
    }

}
 
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!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones