• 从相册选取图片


    关键代码:

    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"/>
  • 相关阅读:
    word2vec原理
    tensorboard
    更换pip源到国内镜像
    pycharm打包exe
    whl文件下载
    pycharm连git和gitee
    Django基础
    mysql相关
    安装anaconda及pytorch
    VSCode 配置python
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6747235.html
Copyright © 2020-2023  润新知