zoukankan      html  css  js  c++  java
  • 3个步骤教你用listView显示网络图片

    1. 从网络获取图片

     

    2. 读取流中数据

    3. 在ListView中显示

    public class MainActivity extends Activity {
        private EditText pathText;
        private ImageView imageView;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            pathText = (EditText) this.findViewById(R.id.path);
            imageView = (ImageView) this.findViewById(R.id.imageView);
            Button button = (Button) this.findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                
                public void onClick(View v) {
                    String path = pathText.getText().toString();
                    try {
                        byte[] data = ImageService.getImage(path);
                        if(data!=null){
                            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//构造一张位图
                            imageView.setImageBitmap(bitmap);//完成图片的显示
                        }else{
                            Toast.makeText(MainActivity.this, R.string.imageerror, 1).show();
                        }
                    } catch (Exception e) {
                        Toast.makeText(MainActivity.this, R.string.neterror, 1).show();
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    大功告成!

  • 相关阅读:
    js学习总结----案例之拖拽
    面向对象-数据属性
    Apply和call方法-扩充函数赖以生存的作用域
    JS中的function
    JS数组
    JS需要注意的细节和一些基础知识
    策略模式+简单工厂模式
    多态
    MVC3学习 八 Action和result过滤器及日志处理
    MVC3学习 七 JQuery方式和微软自带的AJAX请求
  • 原文地址:https://www.cnblogs.com/dartagnan/p/2003458.html
Copyright © 2011-2022 走看看