使用Android系统调用裁剪图片并保存
16lz
2021-01-25
顺序:打开图片--->获取图片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.附上完整代码
更多相关文章
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- android 调用Camera,获取预览帧中的图像
- 设备管理应用程序的实现(Device Administration )
- Android客户端处理服务器端返回的Json格式数据
- Android第七个功能:XmlPullParser添加内容保存为XML文件
- SQLite3命令行使用查询android的telephone数据库
- android用户界面之Gallery3D学习资料汇总