0

我正在尝试从 URL 获取位图,但我不知道该怎么做。这是我正在使用的网址:

String imgurl = "http://api.twitter.com/1/users/profile_image/859486608";

这是我的方法

public  Bitmap loadBitmap(String imgPath) {
    String imgUrlStr = imgPath;
    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(imgUrlStr).getContent());
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

这是日志输出:

11-08 14:41:23.471: WARN/System.err(2682): java.io.IOException: Stream is closed
11-08 14:41:23.481: WARN/System.err(2682):     at java.io.BufferedInputStream.read(BufferedInputStream.java:316)
11-08 14:41:23.481: WARN/System.err(2682):     at java.io.FilterInputStream.read(FilterInputStream.java:130)
11-08 14:41:23.481: WARN/System.err(2682):     at com.ideal.self.Exa_LoadImageUrlActivity.loadImageFromUrl(Exa_LoadImageUrlActivity.java:73)
11-08 14:41:23.481: WARN/System.err(2682):     at com.ideal.self.Exa_LoadImageUrlActivity.onCreate(Exa_LoadImageUrlActivity.java:35)
11-08 14:41:23.481: WARN/System.err(2682):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-08 14:41:23.481: WARN/System.err(2682):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-08 14:41:23.491: WARN/System.err(2682):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-08 14:41:23.491: WARN/System.err(2682):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-08 14:41:23.491: WARN/System.err(2682):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-08 14:41:23.491: WARN/System.err(2682):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 14:41:23.491: WARN/System.err(2682):     at android.os.Looper.loop(Looper.java:123)
11-08 14:41:23.491: WARN/System.err(2682):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-08 14:41:23.491: WARN/System.err(2682):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 14:41:23.491: WARN/System.err(2682):     at java.lang.reflect.Method.invoke(Method.java:521)
11-08 14:41:23.491: WARN/System.err(2682):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-08 14:41:23.501: WARN/System.err(2682):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-08 14:41:23.501: WARN/System.err(2682):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

最好使用异步任务从URL加载图像……试试这个方法:

ImageDownloader().execute(your image URL);

private class ImageDownloader extends AsyncTask<String, Integer, Bitmap>
{
    protected void onPreExecute()
    {
    }

    @Override
    protected Bitmap doInBackground(String... params)
    {
        try
        {
            URL url = new URL(params[0]);
            HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
            if(httpCon.getResponseCode() != 200)throw new Exception("Failed to connect");
            InputStream is = httpCon.getInputStream();
            return BitmapFactory.decodeStream(is);
        }catch(Exception e){
            Log.e("Image","Failed to load image",e);
        }
        return null;
    }

    protected void onPostExecute(Bitmap img){
        ImageView iv = (ImageView)findViewById(R.id.img);
        if(iv!=null && img!=null)
        {
            iv.setImageBitmap(img);
        }
    }
}

我希望这个能帮上忙 :)

于 2012-11-08T09:31:15.077 回答
0

只需从ImageLoader.java中获取一个 ImageLoader 类(libs-for-android - Android 平台的库集合)。

如下进行构造函数调用。

imageLoader = new ImageLoader(YourActivityName.this);

并使用以下代码显示图像。

imageView.setTag(ImageURL);
imageLoader.DisplayImage(ImageURL, ActivityName.this, imageView);
于 2012-11-08T10:12:55.900 回答