URL에서 이미지를 다운로드하고 다운로드 한 이미지를 UI의 이미지보기에 표시해야합니다.멀티 스레딩을 통한 이미지 다운로드가 Android에서 작동하지 않습니다.
public class ShowUIData extends AsyncTask<Void, Void, Void> {
String productvalues[];
Drawable productimagebitmap;
@Override
protected Void doInBackground(Void... params) {
productvalues = hb.getProductDetailsWithJson(id + 1);
if (productvalues != null) {
productimagebitmap = getImage(productvalues[3]);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (productvalues != null) {
// Set the values obtained from the database.
// Check if image returned from URL is not null.
if (productimagebitmap != null) {
ImageView productimage = (ImageView) findViewById(R.id.productimage);
productimage.setImageDrawable(productimagebitmap);
}
}
dismissDialog();
}
// Download image from URL obtained for database.
private Drawable getImage(String address) {
try {
Log.i("product details", "starting image download");
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Drawable d = Drawable.createFromStream(is, "src name");
is.close();
return d;
} catch (Exception e) {
Log.i("the url", address);
e.printStackTrace();
return getApplicationContext().getResources().getDrawable(
R.drawable.noimage);
}
}
유효한 URL이 된 GetImage 함수에 전달되지 않고, 예외가 throw되고 있지가 여전히 이미지가 이미지 뷰에 설정되지 않는이 들어 은 내가 아래에 언급 된 코드를 사용하고 있습니다. 내 응용 프로그램을 디버깅하면 이미지가 올바르게 설정됩니다. 이미지가 다운로드되고 나서 image.setImageDrawable을 호출 할 때까지 블로킹 호출을해야한다고 생각합니다.
여기에 어떤 문제가 발생합니까? 이미지를로드 할 수없는 이유와 디버깅 할 때만 이미지가 표시되는 이유를 파악할 수 없습니다.
미리 감사드립니다.
onPostExecute는 UI 스레드에서 실행됩니다. 그래서 UI 스레드에서 아무것도하지 않습니다. – user590849