我正在尝试从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)
答案 0 :(得分: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);
}
}
}
我希望这会有所帮助:)
答案 1 :(得分:0)
只需从 ImageLoader.java ( libs-for-android - Android平台的库集合)中获取ImageLoader类。
按如下所示进行构造函数调用。
imageLoader = new ImageLoader(YourActivityName.this);
使用以下代码显示图像。
imageView.setTag(ImageURL);
imageLoader.DisplayImage(ImageURL, ActivityName.this, imageView);