zoukankan      html  css  js  c++  java
  • Android打开图库,选择一张图片

    public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
             
            Button button = (Button)findViewById(R.id.b01); 
            button.setText("选择图片"); 
            button.setOnClickListener(new Button.OnClickListener(){ 
                @Override 
                public void onClick(View v) { 
                    Intent intent = new Intent(); 
                    /* 开启Pictures画面Type设定为image */ 
                    intent.setType("image/*"); 
                    /* 使用Intent.ACTION_GET_CONTENT这个Action */ 
                    intent.setAction(Intent.ACTION_GET_CONTENT);  
                    /* 取得相片后返回本画面 */ 
                    startActivityForResult(intent, 1); 
                } 
                 
            }); 
        } 
         
        @Override 
        protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
            if (resultCode == RESULT_OK) { 
                Uri uri = data.getData(); 
                Log.e("uri", uri.toString()); 
                ContentResolver cr = this.getContentResolver(); 
                try { 
                    Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); 
                    ImageView imageView = (ImageView) findViewById(R.id.iv01); 
                    /* 将Bitmap设定到ImageView */ 
                    imageView.setImageBitmap(bitmap); 
                } catch (FileNotFoundException e) { 
                    Log.e("Exception", e.getMessage(),e); 
                } 
            } 
            super.onActivityResult(requestCode, resultCode, data); 
        }  
  • 相关阅读:
    Windows API的CreateFile()中的OPEN_ALWAYS和CREATE_ALWAYS之间的差异
    Linux下的虚拟串口对(可用于在本机上模拟串口进行调试)
    [转&精]IO_STACK_LOCATION与IRP的一点笔记
    【转】在WIN7、WIN10操作系统用WebDAV映射网络驱动器需要的操作
    Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information
    分布式处理大数据的目录及学习树
    利用docker将传统企业集中式架构转换为微服务架构的实践经验
    docker学习笔记5 端口映射与容器互联
    docker学习笔记4 数据管理、持久化
    docker学习笔记3 容器 container
  • 原文地址:https://www.cnblogs.com/yangzhenyu/p/2219405.html
Copyright © 2011-2022 走看看