Anroid调用系统相机或相册获取图片

来源:互联网 发布:马库斯·斯马特数据 编辑:程序博客网 时间:2024/06/05 16:45

选择获取方式

CharSequence[] items = {"相册", "相机"};       new AlertDialog.Builder(this)      .setTitle("选择图片来源")      .setItems(items, new OnClickListener() {          public void onClick(DialogInterface dialog, int which) {              if( which == SELECT_PICTURE ){                  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);                  intent.addCategory(Intent.CATEGORY_OPENABLE);                  intent.setType("image/*");                  startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);               }else{                  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                    startActivityForResult(intent, SELECT_CAMER);                }          }      })      .create().show

Intent方法回掉

protected void onActivityResult(int requestCode, int resultCode, Intent data) {      super.onActivityResult(requestCode, resultCode, data);      if(resultCode == RESULT_OK){          //选择图片          Uri uri = data.getData();           ContentResolver cr = this.getContentResolver();           try {              if(bmp != null)//如果不释放的话,不断取图片,将会内存不够                  bmp.recycle();              bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));          } catch (FileNotFoundException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }          System.out.println("the bmp toString: " + bmp);          imageSV.setBmp(bmp);      }else{          Toast.makeText(SetImageActivity.this, "请重新选择图片", Toast.LENGTH_SHORT).show();      }  }  
0 0
原创粉丝点击