アプリケーションに画像をダウンロードして、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();
}
このコードは、画像をデバイスにダウンロードして保存するために正しく機能しています。ただし、問題は、画像が完全にダウンロードされない場合があることです。このような場合の対処方法は?応答コードが200ですが、画像が正しくダウンロードされていません。その場合、画像はこんな感じになります
誰かが状況を正しく処理する方法を教えてもらえますか?データの書き込み中にアプリが予期せず終了した場合、その状況をどのように処理しますか?その場合も、画像が部分的にディスクに書き込まれ、画像が画像ビューに正しく読み込まれません。
また、ImageViewに画像を入力する際に、画像が正しく読み込まれているかどうかを確認できるかどうかを教えてください。