2013-04-24 63 views
1

我正在使用下面的代码在Android Pager中加载图像,但是我得到一个空白区域而不是图像,当我加载文本时,我得到一个输出。无法在Android适配器中加载图像

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View vi=container; 
     //if(container==null) 
      vi = inflater.inflate(R.layout.fragment_pager_list, null); 

     TextView title = (TextView)vi.findViewById(R.id.textTitle); // title 
     ImageView thumb_image=(ImageView)vi.findViewById(R.id.imageProduct); 
      // thumb image 

     HashMap<String, String> song = new HashMap<String, String>(); 
     song = ListProduct.productList.get(mNum); 

     // Setting all values in listview 
     title.setText(song.get(ListProduct.KEY_TITLE)); 
     Bitmap bitmap = BitmapFactory.decodeFile 
              (song.get(ListProduct.KEY_THUMB_URL)); 

     thumb_image.setImageBitmap(bitmap); 
     return vi; 

    } 

上面的代码我使用但我没有得到我错的地方。请帮帮我。

+0

如果从文件加载,检查文件是否存在于本地,尽量uotput的THUMB_URL控制台。 – Demonick 2013-04-24 11:26:48

+0

请添加布局xml,并且ListProduct代码 – tbkn23 2013-04-24 11:32:44

+0

ListProduct.KEY_THUMB_URL它是一个Url? (如http://www.site.com/img.jpg)?或一个资源到你的应用程序的Android? – VincentLamoute 2013-04-24 11:44:39

回答

0

试试这个....

try { 
     URL url = new URL(ListProduct.KEY_THUMB_URL); 
     HttpGet httpRequest = null; 

     try { 
      httpRequest = new HttpGet(url.toURI()); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); 

     HttpEntity entity = response.getEntity(); 
     BufferedHttpEntity b_entity = new BufferedHttpEntity(entity); 
     InputStream input = b_entity.getContent(); 

     bitmap = BitmapFactory.decodeStream(input); 

     //// Set Bitmap to Imageview 
     thumb_image.setImageBitmap(bitmap); 

    } catch (MalformedURLException e) { 
     Log.d("log", "bad url"); 
    } catch (IOException e) { 
     Log.d("log", "io error"); 
    } 
+0

复制从http://androiddev.orkitra.com/?p=43429 – 2013-04-24 12:55:01

+0

@尼克希尔 - 你可以弹出聊天:http://chat.stackoverflow.com/rooms/28819 – Kev 2013-04-24 13:45:36

+0

然后,第二天,Gowtham,在这2008线程http://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a-file-in-c/381529#381529对凯夫说,他从另一个网站复制了他的答案,实际上是一个刮板网站。版权问题Gowtham? – 2013-05-11 20:11:36