权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"> </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//布局
<ImageView android:id="@+id/iv_photo" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="caijian" android:text="打开相机裁剪" />
public class MainActivity extends AppCompatActivity { private ImageView img_view; private Button butt_11; private ImageView iv_photo; private Uri uri; private String mFilePath; private static int REQUEST_CAMERA_1 = 1; private static int REQUEST_CAMERA_2 = 2; private static int RESULT_LOAD_IMAGE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // butt_11 = (Button) findViewById(R.id.butt_11); iv_photo= (ImageView) findViewById(R.id.iv_photo); ///图库加载 butt_11.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent,RESULT_LOAD_IMAGE); } }); } public void caijian(View view){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机 startActivityForResult(intent, REQUEST_CAMERA_1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { // 如果返回数据 if (requestCode == REQUEST_CAMERA_1) { // 判断请求码是否为REQUEST_CAMERA,如果是代表是这个页面传过去的,需要进行获取 Bundle bundle = data.getExtras(); // 从data中取出传递回来缩略图的信息,图片质量差,适合传递小图片 Bitmap bitmap = (Bitmap) bundle.get("data"); // 将data中的信息流解析为Bitmap类型 iv_photo.setImageBitmap(bitmap);// 显示图片 } else if (requestCode == REQUEST_CAMERA_2) { FileInputStream fis = null; try { fis = new FileInputStream(mFilePath); // 根据路径获取数据 Bitmap bitmap = BitmapFactory.decodeStream(fis); iv_photo.setImageBitmap(bitmap);// 显示图片 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { fis.close();// 关闭流 } catch (IOException e) { e.printStackTrace(); } } } } } ////