Android下载Zip到SD卡,zip不读

时间:2011-01-13 11:09:12

标签: java android web-services sd-card read-write

尝试使用以下代码将zip下载到SD卡,我得到NullpointerException。当我尝试了一些审讯时,我才知道zip文件实际上没有被下载。所以,请您帮助代码是否需要更改或者zip有什么问题吗? M只坚持这一点。请帮忙...我的代码如下......

    {
        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();
    }

}

1 个答案:

答案 0 :(得分:0)

您正在使用空字节数据从流中读取;

    byte[] b = null;
    input.read(b);
    ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b);

评论这些行。