从URL的Andr​​oid填充的ImageView [英] Android fill ImageView from URL

查看:128
本文介绍了从URL的Andr​​oid填充的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试图将图像从我曾尝试加载它作为一个位图,但没有呈现出URL添加到一个ImageView的。因此,没有人知道该怎么做这个最好的方法是什么,或我做错了?

我的继承人code

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    //检查preferences这台UI
    的setContentView(R.layout.singlenews);
      TextView的HEADERTEXT =(的TextView)findViewById(R.id.header_text);
       headerText.setText(最新消息);
     PostTask posttask;
       posttask =新PostTask();
       posttask.execute();
}公共无效loadNews(){    newsStr = getIntent()getStringExtra(singleNews);
    尝试{
        JSONObject的OBJ =新的JSONObject(newsStr);
        内容= obj.getString(内容);
          标题= obj.getString(标题);
          全名= obj.getString(全名);
          创建= obj.getString(创建);
        NewsImageURL = obj.getString(image_primary);
        标语= obj.getString(标语);        元=发表:+全名++创建的;
        网址aURL =新的URL(NewsImageURL);
        康涅狄格州的URLConnection = aURL.openConnection();
        conn.connect();
        InputStream为= conn.getInputStream();
        / *缓冲总是好的性能加分。 * /
        二BufferedInputStream为=新的BufferedInputStream(是);
        / *德code URL数据为位图。 * /
         BM = BitmapFactory.de codeStream(之二);
        bis.close();
        is.close();
        / *将位图到将要返回的ImageView的。 * /
        Log.v(LC,CONTENT =+内容);
        Log.v(LC,标题=+称号);
        Log.v(LC,全名=+全名);
        Log.v(LC,创建=+创建);
        Log.v(LC,NewsImage =+ NewsImageURL);
        Log.v(LC,元=+间位);
        Log.v(LC,标语=+标语);}赶上
 (JSONException E){
    // TODO自动生成catch块
    e.printStackTrace();
}赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
}}
公共类PostTask扩展的AsyncTask<无效,字符串,布尔值> {    @覆盖
    保护布尔doInBackground(虚空...... PARAMS){
        布尔结果= FALSE;
        loadNews();
        publishProgress(进步);
        返回结果;
    }    保护无效onProgressUpdate(字符串...进度){
        StringBuilder的海峡=新的StringBuilder();
            的for(int i = 1; I< progress.length;我++){
                str.append(进度[I] +);            }
    }        @覆盖
    保护无效onPostExecute(布尔结果){
        super.onPostExecute(结果);
        Log.v(BGThread,开始FILLIN数据);        fillData();        }
}公共无效fillData(){    新闻浏览= LayoutInflater.from(getBaseContext())。膨胀(R.layout.newsdetailact,
            空值);  TextView的标题=(TextView中)NewsView.findViewById(R.id.NewsTitle);
  Title.setText(职称);  TextView的标语=(TextView中)NewsView.findViewById(R.id.subtitle);
  Tagline.setText(标语);  TextView的元数据=(TextView中)NewsView.findViewById(R.id.meta);
  MetaData.setText(甲基);
     ImageView的NewsImage =(ImageView的)NewsView.findViewById(R.id.imageView2);
     NewsImage.setImageBitmap(BM);  TextView的日程地址搜索Maincontent =(TextView中)NewsView.findViewById(R.id.maintext);
  MainContent.setText(内容);  Log.v(BGThread,填充效果);适配器=新MergeAdapter();adapter.addView(新闻浏览);
setListAdapter(适配器);
}}


解决方案

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

 网​​址URL =新的URL(http://image10.bizrate-images.com/resize?sq=60&uid=2216744464);
BMP位图= BitmapFactory.de codeStream(url.openConnection()的getInputStream());
imageView.setImageBitmap(BMP);

T认为它帮助你。

感谢

Hi i'm trying to add an image to an ImageView from a URL i have tried loading it as a bitmap but nothing is showing. so does anyone know what the best method to do this is or what i'm doing wrong?

heres my code

@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);


}



}

解决方案

Please use this code spinet convert url images to bitmap and show it in image view .

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 think it help you.

Thanks

这篇关于从URL的Andr​​oid填充的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆