作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在我的应用程序中下载图像以将其填充到 UI 中。
这是我用来下载该图像并将其保存到设备外部存储器的代码。
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();
}
最佳答案
我也在这方面做了很多研究,最后我通过检查图像长度来克服这个问题
URL url = new URL(url);
URLConnection conection = url.openConnection();
conection.connect();
fileLength = conection.getContentLength();
fileLength
和下载的图像文件长度。如果相等或接近相等,则下载成功。
关于android - 如何在android中处理不完整的图像下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671443/
我是一名优秀的程序员,十分优秀!