2012-06-15 83 views
1

您好我想从一个URL添加图像到ImageView我已经尝试加载它作为位图,但没有显示。所以有谁知道什么是最好的方法来做到这一点或我做错了什么?Android填充ImageView从URL

继承人我的代码

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    //Check Preferences which sets UI 
    setContentView(R.layout.singlenews); 
     TextView headerText = (TextView) findViewById(R.id.header_text); 
     headerText.setText("Latest News"); 


    PostTask posttask; 
     posttask = new PostTask(); 
     posttask.execute(); 
} 

public void loadNews(){ 

    newsStr = getIntent().getStringExtra("singleNews"); 




    try { 
     JSONObject obj = new JSONObject(newsStr); 
     content = obj.getString("content"); 
      title = obj.getString("title"); 
      fullName = obj.getString("fullname"); 
      created = obj.getString("created"); 
     NewsImageURL = obj.getString("image_primary"); 
     tagline = obj.getString("tagline"); 

     meta = "posted by: " + fullName + " " + created; 


     URL aURL = new URL("NewsImageURL"); 
     URLConnection conn = aURL.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     /* Buffered is always good for a performance plus. */ 
     BufferedInputStream bis = new BufferedInputStream(is); 
     /* Decode url-data to a bitmap. */ 
     bm = BitmapFactory.decodeStream(bis); 
     bis.close(); 
     is.close(); 
     /* Apply the Bitmap to the ImageView that will be returned. */ 






     Log.v("lc", "content=" + content); 
     Log.v("lc", "title=" + title); 
     Log.v("lc", "fullname=" + fullName); 
     Log.v("lc", "created=" + created); 
     Log.v("lc", "NewsImage=" + NewsImageURL); 
     Log.v("lc", "Meta=" + meta); 
     Log.v("lc", "tagline=" + tagline); 







} catch 
(JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

} 




public class PostTask extends AsyncTask<Void, String, Boolean> { 

    @Override 
    protected Boolean doInBackground(Void... params) { 
     boolean result = false; 
     loadNews(); 
     publishProgress("progress"); 
     return result; 
    } 

    protected void onProgressUpdate(String... progress) { 
     StringBuilder str = new StringBuilder(); 
      for (int i = 1; i < progress.length; i++) { 
       str.append(progress[i] + " "); 

      } 
    } 

     @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     Log.v("BGThread", "begin fillin data"); 

     fillData(); 

     } 
} 

public void fillData(){ 

    NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.newsdetailact, 
      null); 

    TextView Title = (TextView) NewsView.findViewById(R.id.NewsTitle); 
    Title.setText(title); 

    TextView Tagline = (TextView) NewsView.findViewById(R.id.subtitle); 
    Tagline.setText(tagline); 

    TextView MetaData = (TextView) NewsView.findViewById(R.id.meta); 
    MetaData.setText(meta); 




    ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2); 
    NewsImage.setImageBitmap(bm); 

    TextView MainContent = (TextView) NewsView.findViewById(R.id.maintext); 
    MainContent.setText(content); 





    Log.v("BGThread", "Filled results"); 

adapter = new MergeAdapter(); 

adapter.addView(NewsView); 


setListAdapter(adapter); 


} 



} 
+0

列表使用懒惰图像加载器...搜索....... –

+0

它不是一个列表抱歉它只是一个图像 –

+0

什么是网址? URL aURL =新网址(“NewsImageURL”); –

回答

3

请使用此代码尖晶石URL图像转换为位图,并显示在图像视图。

URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464"); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
imageView.setImageBitmap(bmp); 

T认为它可以帮助你。

感谢

+0

但如何添加它时扩展到片段我的图像正在填充@Md Abdul Gafur –

0
  String url1 = "Your URL...."; 

        URL ulrn = new URL(url1); 
        HttpURLConnection con = (HttpURLConnection) ulrn 
          .openConnection(); 
        InputStream is = con.getInputStream(); 
        bmp1 = BitmapFactory.decodeStream(is); 

        int widthPx = 150; //you can set width 
        int heightPx = 150; //you can set height 
        bmp1=Bitmap.createScaledBitmap(bmp1, widthPx, heightPx, true); 

     Imgview1.setImageBitmap(bmp1); 
0

在这种情况下,我会尝试将InputStream写入文件,然后从文件加载。这通常是有效的。证明写入文件的另一个理由是因为我更喜欢从网络上延迟加载图像,因为它可能需要比预期更长的时间。在这种情况下,我怀疑InputStream在它有机会完成该过程之前已关闭。

你的代码和我的结构几乎一样,没有复制到文件部分。

如果要调查,我建议你这样做:

  1. 检查图像确实存在。
  2. 检查并比较流和位图的大小。
  3. 检查InpuStream以查看它是否意外关闭。