日期:2014-05-16  浏览次数:20449 次

android错误之database disk image is malformed (code 11)

做来电显示归属地和查询归属地功能的时候,需要从服务器下载数据库,

但是下载之后,查询总是报错database disk image is malformed (code 11)

开始以为是查询语句不对,后来意识到,跟之前解析包时出现错误是一样的问题,

下载过程中格式损坏了。

于是解决方法跟http://blog.csdn.net/jason0539/article/details/21742019采取了一样的策略,

把每次读取的数组缩小,代码如下:

public class DownloadTask {  
      
    /** 
     * @param path下载地址 
     * @param filePath存储路径 
     * @param progressDialog进度条 
     * @return 
     * @throws Exception 
     */  
    public static File getFile(String path,String filePath,ProgressDialog progressDialog) throws Exception{  
          
        URL url = new URL(path);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        connection.setConnectTimeout(2000);  
        connection.setRequestMethod("GET");  
        if(connection.getResponseCode() == 200){  
            int total = connection.getContentLength();  
            progressDialog.setMax(total);  
              
            InputStream is = connection.getInputStream();//获得socket输入流  
            File file = new File(filePath);  
            FileOutputStream fos = new FileOutputStream(file);//file输出流  
            byte[] buffer = new byte[1024];  
            int len;  
            int progress = 0;  
            while((len = is.read(buffer)) != -1){  
                fos.write(buffer);  
                progress += len;  
                progressDialog.setProgress(progress);  
            }  
            fos.flush();  
            is.close();  
            fos.close();  
            connection.disconnect();  
            return file;  
        }  
        return null;  
    }  


把里面这行里的1024改小就可以避免损坏了,

 byte[] buffer = new byte[1024];  

由于总是错,我甚至改成了

byte[] buffer = new byte[8];


这样是没错了,但是有个弊端,下载变的很慢,下载的数据库是16M的,奇慢无比,

临时性这样解决了。

 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)