Android旋转本地图片并保存
来源:互联网 发布:syslog 端口号 编辑:程序博客网 时间:2024/06/14 05:06
之前写的《Android获取SD卡上所有图片、视频缩略图和音乐专辑封面》获取到了图片并显示,现在需要对图片进行旋转操作,并且要保存旋转后的图片。
分为三个步骤:
1.获得图片文件的路径及Bitmap格式的图片
2.旋转图片一定角度,得到新图片
3.把新图片保存到原来的路径,即覆盖原来的图片保存
1.获得图片文件的路径及Bitmap格式的图片
根据之前写的《Android获取SD卡上所有图片、视频缩略图和音乐专辑封面》很容易就完成第一步:
//获取图片Bitmap picture = BitmapFactory.decodeFile(mFileInfo.getFilePath());
2.旋转图片一定角度,得到新图片
用Matrix来旋转图片。
/** * @Description 旋转图片一定角度 * @param bitmap 要旋转的图片 * @param degree 要旋转的角度 * @return 旋转后的图片 */public Bitmap rotatePicture(final Bitmap bitmap, final int degree) { Matrix matrix = new Matrix(); matrix.postRotate(degree); Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);}
3.把新图片保存到原来的路径,即覆盖原来的图片保存
/** * @Description 保存图片到指定路径 * @param bitmap 要保存的图片 * @param filePath 目标路径 * @return 是否成功 */public boolean saveBmpToPath(final Bitmap bitmap, final String filePath) { if (bitmap == null || filePath == null) { return false; } boolean result = false; //默认结果 File file = new File(filePath); OutputStream outputStream = null; //文件输出流 try { outputStream = new FileOutputStream(file); result = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); //将图片压缩为JPEG格式写到文件输出流,100是最大的质量程度 } catch (Exception e) { e.printStackTrace(); } finally { if (outputStram != null) { try { outputStream.close(); //关闭输出流 } catch (IOException e) { e.printStackTrace(); } } } return result;}
示例:完整调用
以下为我项目用到的代码片段,其中FileInfo
为自定义数据结构
public boolean doRotate(final int degree) { //得到Gallery当前选中的item的数据 FileInfo fileInfo = (FileInfo) mPictureGallery.getSelectedItem(); //第一步 Bitmap picture = BitmapFactory.decodeFile(fileInfo.getFilePath()); //第二步 Bitmap resizePicture = rotatePicture(picture, degree); //第三步 saveBmpToPath(resizePicture, fileInfo.getFilePath()); //更新Gallery显示 mPictureGallery.updateData();}
Reference:《Android 图片Bitmap保存到内存卡》
如果你又任何问题,欢迎留言告诉我!
0 0
- Android旋转本地图片并保存
- Android 旋转本地图片并保存覆盖原图片
- 旋转图片并保存
- Android画图并保存图片到本地
- android 选择本地图片上传到服务端并保存
- android 从服务器下载图片,并保存到本地
- Android下载网络图片并保存在本地相册
- android 照片旋转并保存
- Android图片保存到本地,并再从本地获取图片
- Android图片保存到本地,并再从本地获取图片
- Android 保存图片到本地
- android保存图片到本地
- Android保存图片到本地
- Android保存图片到本地
- Android保存图片到本地
- Android 保存图片到本地
- android 读取本地或网络图片,转换为Bitmap 并保存本地中
- listView显示网路图片,并保存本地
- android 录制音频的时候,调用麦克风出停止
- 【Android】intent传递数据时,数据大小的限制问题
- windows笔记-虚拟内存
- LeetCode_67---Add Binary
- Google将不再支持Android Eclipse Tools
- Android旋转本地图片并保存
- js 支持兼容性的trim操作
- Android 为LinearLayout增加分割线 divider
- html 中文乱码
- ios构造函数
- C++关于二进制位操作小结
- xdebug安装和使用
- Hive配置项的含义详解(5)
- JAVA 毫秒时间转字符显示