0

我从在线 xml 文件中解析了几项天气数据。其中一个节点是我要显示的图像的 URL。我设法解析它并将其保存为字符串变量,并在应用程序中显示为字符串。如何让它显示图像而不是文本?提前致谢!

4

2 回答 2

1

首先,您需要下载图像并将其存储在 Bitmap 对象中。

然后,用 ImageView 显示它。

这个答案详细描述了如何做到这一点

于 2012-10-17T12:38:29.070 回答
0
public class MainActivity extends Activity {
   ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pd = new ProgressDialog(MainActivity.this);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
    .execute("Your URL");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
        }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
          pd = new ProgressDialog(MainActivity.this);
          pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    }



    protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
    InputStream in = new java.net.URL(urldisplay).openStream();
    mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
    Log.e("Error", e.getMessage());
    e.printStackTrace();
    }
    return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        pd.dismiss();
    bmImage.setImageBitmap(result);
    }
    }         
 }
于 2012-10-17T13:09:49.243 回答