2012-12-14 30 views
0

我正試圖在ListView上顯示圖像。這段代碼:使用位圖時沒有任何東西

private String[] movieURL = {"..","..",".."}; 

try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(movieURL[position]).getContent()); 
      holder.image.setImageBitmap(bitmap); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

這裏是類ViewHolder:

private static class ViewHolder { 
    public TextView text1; 
    public TextView text2; 
    public ImageView image; 
    } 

但是這說明不了什麼。我認爲代碼是好的。和堆棧跟蹤:

12-14 07:50:54.975: D/dalvikvm(1865): GC_CONCURRENT freed 122K, 8% free 3082K/3324K, paused 4ms+28ms, total 123ms 
12-14 07:50:55.175: D/dalvikvm(1865): GC_FOR_ALLOC freed 16K, 8% free 3178K/3424K, paused 40ms, total 51ms 
12-14 07:50:55.195: I/dalvikvm-heap(1865): Grow heap (frag case) to 4.291MB for 1127536-byte allocation 
12-14 07:50:55.286: D/dalvikvm(1865): GC_FOR_ALLOC freed <1K, 6% free 4278K/4528K, paused 84ms, total 86ms 
12-14 07:50:55.395: D/dalvikvm(1865): GC_CONCURRENT freed <1K, 6% free 4279K/4528K, paused 10ms+20ms, total 118ms 
12-14 07:50:55.547: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0 
12-14 07:50:55.625: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0 
12-14 07:50:55.685: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0 
12-14 07:50:55.715: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0 
12-14 07:50:55.815: W/Trace(1865): Unexpected value from nativeGetEnabledTags: 0 
12-14 07:50:57.125: D/skia(1865): --- SkImageDecoder::Factory returned null 
12-14 07:50:57.875: D/skia(1865): --- SkImageDecoder::Factory returned null 
12-14 07:50:58.736: D/skia(1865): --- SkImageDecoder::Factory returned null 
12-14 07:50:59.386: D/skia(1865): --- SkImageDecoder::Factory returned null 
12-14 07:50:59.945: D/skia(1865): --- SkImageDecoder::Factory returned null 
+0

看到我的回答它會解決你的問題。 –

回答

0

嘗試這樣

holder.image.setImageBitmap(BitmapFactory.decodeFile(movieurl 
       .get(position))); 
+0

仍然不能正常工作.. – user23256

0

你甚至肯定Bitmap -object不爲空?做一些記錄..

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(movieURL[position]).getContent()); 

if(bitmap == null) { 
    Log.i("Aha!", "The Bitmap was null all this time :)"); 
} 

holder.image.setImageBitmap(bitmap); 

如果您Bitmap爲空,檢查的URL實際上指向你試圖獲取資源。

另外,要記住,

<uses-permission android:name="android.permission.INTERNET"/> 
AndroidManifest.xml

。不這樣做會給你一個空的Bitmap沒有任何錯誤。

+0

我的位圖對象爲空。那怎麼可能? – user23256

+0

如果響應的內容類型未知,則'URL.getContent()'返回null。請求的另一端是什麼? – Ole

+0

這是一個url像http://ia.media-imdb.com/images/M/[email protected]@.V1._SX54_CR0,0,54,74_.jpg – user23256

0

嘗試下面的代碼,使用AsyncTask從web下載圖像並在imageview中顯示,它將解決您的問題。

public class MainActivity extends Activity { 

    ImageView mImgView1; 
    static Bitmap bm; 
    ProgressDialog pd; 
    String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg"; 
    BitmapFactory.Options bmOptions; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mImgView1 = (ImageView) findViewById(R.id.mImgView1); 
     pd = ProgressDialog.show(MainActivity.this, "Aguarde...", 
       "Carregando..."); 
     new ImageDownload().execute(""); 
    } 

    public class ImageDownload extends AsyncTask<String, Void, String> { 

     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      bmOptions = new BitmapFactory.Options(); 
      bmOptions.inSampleSize = 1; 
      loadBitmap(imageUrl, bmOptions); 
      return imageUrl; 
     } 

     protected void onPostExecute(String imageUrl) { 
      pd.dismiss(); 
      if (!imageUrl.equals("")) { 
       mImgView1.setImageBitmap(bm); 
      } else { 
       Toast.makeText(MainActivity.this, 
         "Não foi possível obter resultados", Toast.LENGTH_LONG) 
         .show(); 
      } 
     } 

    } 

    public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) { 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bm = BitmapFactory.decodeStream(in, null, options); 
      in.close(); 
     } catch (IOException e1) { 
     } 
     return bm; 
    } 

    private static InputStream OpenHttpConnection(String strURL) 
      throws IOException { 
     InputStream inputStream = null; 
     URL url = new URL(strURL); 
     URLConnection conn = url.openConnection(); 

     try { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       inputStream = httpConn.getInputStream(); 
      } 
     } catch (Exception ex) { 
     } 
     return inputStream; 
    } 
}