使用Android系统调用裁剪图片并保存

来源:互联网 发布:浙江网络作家协会 编辑:程序博客网 时间:2024/05/14 01:57


顺序:打开图片--->获取图片Uri或者Bitmap--->打开系统裁剪框--->获得裁剪后的图片的Uri或者Bitmap。

1.单纯打开图库选择图片

Intent intent = new Intent();intent.setAction(Intent.ACTION_GET_CONTENT);// 打开图库获取图片intent.setAction(Intent.ACTION_PICK);// 打开图库获取图片intent.setType("image/*");// 这个参数是确定要选择的内容为图片intent.putExtra("return-data", true);// 是否要返回,如果设置false取到的值就是空值startActivityForResult(intent, REQUEST);


1.1选择图片并显示在ImageView

Uri selectImg = data.getData();ContentResolver contentResolver = this.getContentResolver();try {bm = BitmapFactory.decodeStream(contentResolver.openInputStream(selectImg));normal_img.setImageBitmap(bm);} catch (FileNotFoundException e) {e.printStackTrace();}


1.2拍照图片

File cameraFile = new File(Environment.getExternalStorageDirectory().getPath(),"abc.jpg");if (cameraFile.exists()) {cameraFile.delete();}Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));startActivityForResult(intent2, TAKE_PICTURE);


2.选择图库里的图片进行裁剪

Intent antent = new Intent(Intent.ACTION_PICK, null);antent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//限制图片类型,可写"image/jpg"或"image/png"

//MediaStore.Images.Media.EXTERNAL_CONTENT_URI意思是返回的数据类型是图片对应的Uri,不是将Bitmap直接返回。如果直接返回Bitmap,如果Bitmap太大,系统会强行压缩造成图片的失真,所以不管图片大小,最好返回数据都用Uri
startActivityForResult(antent, CUT_REQUEST);

2.1调用裁剪框返回数据为Bimap

/*** 以时间戳命名将bitmap写入文件* * @param bitmap*/public static void writeFileByBitmap2(Bitmap bitmap) {String path = Environment.getExternalStorageDirectory().getAbsolutePath();//手机设置的存储位置File file = new File(path);File imageFile = new File(file, System.currentTimeMillis() + ".png");if (!file.exists()) {file.mkdirs();}try {imageFile.createNewFile();FileOutputStream outputStream = new FileOutputStream(imageFile);bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);outputStream.flush();outputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


2.1调用裁剪框返回数据为Uri

/*** 以时间戳命名将bitmap写入文件* * @param bitmap*/public static void writeFileByBitmap2(Bitmap bitmap) {String path = Environment.getExternalStorageDirectory().getAbsolutePath();//手机设置的存储位置File file = new File(path);File imageFile = new File(file, System.currentTimeMillis() + ".png");if (!file.exists()) {file.mkdirs();}try {imageFile.createNewFile();FileOutputStream outputStream = new FileOutputStream(imageFile);bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);outputStream.flush();outputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


3保存图片

/*** 以时间戳命名将bitmap写入文件* * @param bitmap*/public static void writeFileByBitmap2(Bitmap bitmap) {String path = Environment.getExternalStorageDirectory().getAbsolutePath();//手机设置的存储位置File file = new File(path);File imageFile = new File(file, System.currentTimeMillis() + ".png");if (!file.exists()) {file.mkdirs();}try {imageFile.createNewFile();FileOutputStream outputStream = new FileOutputStream(imageFile);bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);outputStream.flush();outputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}


注意:裁剪或者选择图片,最好返回数据的格式是Uri!!!

4.附上完整代码





0 0
原创粉丝点击