2013-05-09 51 views
1

在网站http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9上,我们可以得到一个名为ibikegif.gif的gif图像。但是我无法在我的android手机上显示它。我的代码是这样的:为什么我无法在Android平台上显示图像

/** 
    *@param url the imageURL.it references to 
    *"http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9" here 
    * 
    **/ 
    public Bitmap returnBitMap(String url) { 
     URL myFileUrl = null; 
     Bitmap bitmap = null; 
     try { 
      myFileUrl = new URL(url); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection conn = (HttpURLConnection) myFileUrl 
        .openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      bitmap = BitmapFactory.decodeStream(is); //Attention:bitmap is null  
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return bitmap; 
    } 

你能帮我吗?

+0

您是否将位图设置为imageview?imageView.setImageBitmap(bitmap); – Raghunandan 2013-05-09 05:58:17

+0

位图为空,所以我无法显示使用ImageView的图像。 – WikiStyle 2013-05-09 06:18:39

+0

当你把'conn.connect'后面的conn.getResponseCode(),它返回什么? – iBecar 2013-05-09 07:43:53

回答

1

我测试过这段代码,它工作。这基本上是你所做的一个区别,我也明确地设置了请求方法GET。

public class MyActivity extends Activity { 

    private ImageView imageView; 
    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     imageView = (ImageView) findViewById(R.id.imageView); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     new AsyncTask<Void, Void, Void>() { 
      Bitmap bitmap; 

      @Override 
      protected Void doInBackground(Void... params) { 
       try{ 
        bitmap = returnBitMap("http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 
       imageView.setImageBitmap(bitmap); 
       imageView.invalidate(); 
      } 

     }.execute(); 
    } 

    public Bitmap returnBitMap(String url) throws IOException { 
     URL myFileUrl = null; 
     Bitmap bitmap = null; 
     InputStream is = null; 
     try { 
      myFileUrl = new URL(url); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
      conn.setDoInput(true); 
      conn.setRequestMethod("GET"); 
      conn.connect(); 
      is = conn.getInputStream(); 
      bitmap = BitmapFactory.decodeStream(is); 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if(is != null) { 
      is.close(); 
     } 
     return bitmap; 
    } 
} 
+0

谢谢@iBecar。我调试你的邮政编码,它不适用于我的手机。我调试代码,'位图= BitmapFactory.decodeStream(是)'位图始终为空。我使用了发布的方式'http:// droid-blog.net/2011/10/15/tutorial-how-to-play-animated-gifs-in-android-%E2%80%93-part-2 /' , 有用。 – WikiStyle 2013-05-11 14:46:50

相关问题