2013-05-09 49 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

相關問題