1

我正在尝试使用ImageView控件显示来自远程 URL 的图像

    private Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
        }
    catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}

设置图像:

Drawable drawable = LoadImageFromWebOperations(Manager.URL());
                imageView.setBackgroundDrawable(drawable);

它可以在 android 2.2 和 3.2 中工作,但不能在 android 4.0.4 中工作?

4

1 回答 1

3

坦克。这段代码解决了我的问题

    public class DownloadImagesTask extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... urls) {
        return download_Image(urls[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        _imgview.setImageBitmap(result);              // how do I pass a reference to mChart here ?
    }


    private Bitmap download_Image(String url) {
        //---------------------------------------------------
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {

        } 
        return bm;
        //---------------------------------------------------
    }


    }

并在活动中使用:

new DownloadImagesTask().execute(url));
于 2013-06-07T08:30:11.353 回答