我正在尝试将图片从网址加载到文字视图,但无法做到,他们没有错误,进度条也出现了,但图片没有加载到图片视图,请帮助,提前谢谢
我的代码..
1。 XML:
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
/>
2。 java code ....
private class Load_Product_Image extends
AsyncTask<Void, Drawable, Drawable> {
private Object fetch_image(String webaddress)
throws MalformedURLException, IOException {
URL url = new URL(webaddress);
Object content = url.getContent();
return content;
}
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity(),ProgressDialog.THEME_TRADITIONAL);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Drawable doInBackground(Void... params) {
InputStream is = null;
try {
is = (InputStream) this.fetch_image(image_url);// image uri
// address
// getting
// from
// previous
// toplist
// activity
// using
// intent
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable draw = Drawable.createFromStream(is, "src");
return draw;
}
protected void onPostExecute(final Drawable result) {
try {
// Display Image...
runOnUiThread(new Runnable() {
public void run() {
profile_image.setImageDrawable(result);
}
});
progressDialog.dismiss();
} catch (Exception e) {
// nothing
}
}