如何在android中处理不完整的图像下载

如何在android中处理不完整的图像下载

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

问题描述

我正在我的应用程序中下载图像以将其填充到UI。

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

此代码正常工作以下载&将图像保存到设备上。但问题是,有时图像没有被完全下载。如何处理这种情况?即使响应代码为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

任何人都可以告诉我如何正确处理情况?如果应用程序在写入数据时意外终止,那么如何处理这种情况?在这种情况下,图像部分写入磁盘和我的图像没有正确地加载到图像视图。

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

检查 fileLength 并下载图像文件长度。如果它相等或接近相等,那么它被成功下载。

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

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

07-26 01:38