如何处理不完整的图像下载android系统? [英] How to handle incomplete image download in android?

查看:151
本文介绍了如何处理不完整的图像下载android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序下载图像来填充它的UI。

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

下面是code我使用的下载和放大器;保存到设备外部存储器的形象。

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

这code正常工作,下载和放大器;到设备保存图像。但问题是,有时图像没有得到完全下载。如何处理此类案件?即使响应code为200,图像是没有得到正确下载。在这种情况下,图像看起来像这样

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

谁能告诉我如何正确处理的情况呢?柜面如果在写入数据,然后如何处理situataion应用程序意外终止?在这种情况下,也像部分写在磁盘和放大器;我的形象不获取到图像视图加载正确。

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.

另外,请告诉我,是否有可能或不检查图像是否正确装入与否而填充它的ImageView的。

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.

推荐答案

我也做了很多的研究在这最后我通过检查图像长度克服这种

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.

这篇关于如何处理不完整的图像下载android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆