zoukankan      html  css  js  c++  java
  • 从相册选取图片

    关键代码:

    public class MainActivity extends Activity {
    
        public static final  int SELECT_PICTURE = 1;
        Bitmap bitmap = null;
        ImageView picture;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            picture = (ImageView)findViewById(R.id.picture);
        }
    
        protected void myClick(View v){
            if( v.getId() == R.id.choose_btn ){
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent,"选择图片"), SELECT_PICTURE );
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case SELECT_PICTURE:
                    Uri uri = data.getData();
                    ContentResolver cr = getContentResolver();
                    try{
                        if (bitmap != null) {
                            bitmap.recycle();
                        }
                // 缩率图参数
                        BitmapFactory.Options opts=new BitmapFactory.Options();
                        opts.inSampleSize=2;
                        bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri),null,opts);
                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }
                    picture.setImageBitmap(bitmap);
                    break;
            }
        }
    }

    权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • 相关阅读:
    我到 vim 配置文件---------修改从---http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html
    Bayer图像处理
    Ubuntu 12.04下安装OpenCV 2.4.2
    vim寄存器与复制粘贴的实现
    window 驱动
    js 面试题
    angular 写的一个分页功能
    angular scope 方法
    avalon 框架
    贴几个常用的基础函数
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747235.html
Copyright © 2011-2022 走看看