안녕하세요 모든 어떻게 이미지를 다운로드 완료 될 때까지 기다릴이 코드를 만들 수 있습니다.android doInBackground
doInBackground(URL... paths)
대체 할 수 있습니다. 다운로드 후 코드 나머지 부분을 계속 기다립니다.
private class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
// This class definition states that DownloadImageTask will take String
// parameters, publish Integer progress updates, and return a Bitmap
protected Bitmap doInBackground(URL... paths) {
URL url;
try {
url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
int length = connection.getContentLength();
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length];
int buffersize = (int) Math.ceil(length/(double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
if (length < buffersize) {
read = is.read(imageData, downloaded, length);
} else if ((length - downloaded) <= buffersize) {
read = is.read(imageData, downloaded, length
- downloaded);
} else {
read = is.read(imageData, downloaded, buffersize);
}
downloaded += read;
publishProgress((downloaded * 100)/length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
length);
if (bitmap != null) {
Log.i(TAG, "Bitmap created");
} else {
Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed exception: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return null;
}
protected void onPostExecute(Bitmap result) {
String name = ImageLink.substring(ImageLink
.lastIndexOf("/") + 1);
if (result != null) {
hasExternalStoragePublicPicture(name);
saveToSDCard(result, name);
isImage = true;
} else {
isImage = false;
}
}
}
귀하의 질문에 이해가되지 않습니다. 'doInBackground()'*는 이미지를 다운로드합니다. 끝내기를 기다릴 수는 없습니다. – CommonsWare
@CommonsWare : 나는 나머지 코드를 다운로드가 끝날 때까지 기다려야한다. 나는 이미지가 끝날 때까지 코드를 계속하고 싶다. 메신저가 1 개 이상의 이미지를 다운로드하고 어떤 이유로 겹쳐서 2 개의 이미지로 끝나기 때문이다. 어떤 것이 ...?! – moe
DownloadImageTask 인스턴스를 만들 코드를 표시하십시오. 여러 인스턴스를 만들고 있습니까? 그렇다면 문제가 될 수 있습니다. – Squonk