Android 4.0.4从远程URL显示图像?

时间:2013-06-06 19:21:49

标签: android

我正在尝试使用ImageView控件

从远程网址显示图片
    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中无效?

1 个答案:

答案 0 :(得分: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));