How to handle incomplete image download in android?

大憨熊 提交于 2019-12-12 20:20:29

问题


I am downloading images in my application to populate it on to the UI.

Here is the code I am using to download & save that image on to the device external memory.

        File firstDirectory=new File(Environment.getExternalStorageDirectory()+"/Catalog");     
        if (firstDirectory.exists()==false) 
        {
            firstDirectory.mkdir();
        }

        for (int i = 0; i <list.size() && !isCancelled(); i++) {

            Content content= list.get(i);
            try 
                {

            File firstFile =new File(firstDirectory+"/"+content.getId()+".jpeg");
            if (firstFile.exists()==false || firstFile.length()==0) 
            {               
                Boolean status=firstFile.createNewFile();   
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(kSubURL+content.getPublisherID()+"&tid="+content.getId()+"_370&tp=i");
                HttpResponse resp = httpClient.execute(httpGet);
                HttpEntity entity= resp.getEntity();
                InputStream is = entity.getContent();
                    FileOutputStream foutS = new FileOutputStream(firstFile);

                    byte[] buffer = new byte[1024];                 
                    long total = 0;
                    int count;
                    while ((count = is.read(buffer)) != -1) {
                        total += count;
                        foutS.write(buffer, 0, count);
                    }

                    foutS.close();
                    is.close();
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

This code is working properly to download & save images on to the device. But the problem is, sometimes images are not getting downloaded completely. How to handle such kind of cases? Even though response code is 200, image is not getting downloaded properly. In that cases, image is looking something like this

Can anyone please tell me how to handle the situation correctly? Incase if app is terminated unexpectedly while writing the data then how to handle that situataion? In that case also, image is partially written on to disk & my image is not getting loaded properly on to the image view.

Also, please tell me whether it is possible or not to check whether image is properly loaded or not while populating it on to the ImageView.


回答1:


I also did lot a research in this and finally i overcome this by checking image length

URL url = new URL(url);
URLConnection conection = url.openConnection();
conection.connect();
fileLength = conection.getContentLength();

check fileLength and downloaded image file length. If it is equal or near to be equal then it is downloaded successfully.



来源:https://stackoverflow.com/questions/12671443/how-to-handle-incomplete-image-download-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!