首页 > 学院 > 开发设计 > 正文

安卓开发选择图片并裁剪

2019-11-06 09:57:31
字体:
来源:转载
供稿:网友
chooseFromAlbum.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.PRintStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent(Intent.ACTION_PICK,null); //此处调用了图片选择器 //如果直接写intent.setDataAndType("image/*"); //调用的是系统图库 intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CUT_PICTURE); } }); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CUT_PICTURE: if (resultCode == RESULT_OK) { //此处启动裁剪程序 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(data.getData(), "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, SHOW_PICTURE); } break; case SHOW_PICTURE: if (resultCode == RESULT_OK) { try { //将output_image.jpg对象解析成Bitmap对象,然后设置到ImageView中显示出来 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver() .openInputStream(imageUri)); picture.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; default: break; } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表