安卓保存图片到SD卡,使用byte流
来源:互联网 发布:淘宝职业退货师被抓 编辑:程序博客网 时间:2024/05/16 06:57
头几天遇到一个问题:在安卓开发应用中保存图片到SD卡,并且 用户在图库中搜到,类似于缓存的那种形式。最开始的第一想法是改一下后缀名,例如把一个图片保存为image1.txt,这样保存当然没问题,但在应用中读取中就不行了,后来也没研究为什么不能正常读取,毕竟这种办法太土鳖了。。。
今天有空上网搜了一下,发现使用byte流保存到SD卡就可以满足我的需求。下面我把正常保存图片文件的代码和保存图片byte流的代码都贴出来,方便大家共同学习参考。
假设我的图片的名字为 image1。
正常保存图片文件的代码(例如image1.png):
public static void savePhotoToSDCard(Bitmap photoBitmap,String path,String photoName){if (checkSDCardAvailable()) {File dir = new File(path);if (!dir.exists()){dir.mkdirs();}File photoFile = new File(path , photoName + ".png");FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream(photoFile);if (photoBitmap != null) {if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {fileOutputStream.flush();//fileOutputStream.close();}}} catch (FileNotFoundException e) {photoFile.delete();e.printStackTrace();} catch (IOException e) {photoFile.delete();e.printStackTrace();} finally{try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}} }
下面是保存图片byte流的代码,这样sd卡就会有一个名为image1的文件。
public static byte[] bitmapToBytes(Bitmap bm) {byte[] bytes = null;if (bm != null) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);bytes = baos.toByteArray();}return bytes;}public static void savePhotoToSDCardByte(Bitmap photoBitmap,String path,String photoName){if (checkSDCardAvailable()) {File dir = new File(path);if (!dir.exists()){dir.mkdirs();}if(photoBitmap !=null){byte[] byteArray = bitmapToBytes(photoBitmap);File photoFile = new File(path , photoName);FileOutputStream fileOutputStream = null;BufferedOutputStream bStream = null;try {fileOutputStream = new FileOutputStream(photoFile);bStream = new BufferedOutputStream(fileOutputStream);bStream.write(byteArray);} catch (FileNotFoundException e) {photoFile.delete();e.printStackTrace();} catch (IOException e) {photoFile.delete();e.printStackTrace();} finally{try {bStream.close();} catch (IOException e) {e.printStackTrace();}}}//(photoBitmap !=null)} }
0 0
- 安卓保存图片到SD卡,使用byte流
- 安卓保存数据到SD卡
- 解决安卓保存图片到SD卡后,相册不显示问题。
- 保存图片到SD卡
- 图片保存到sd卡
- 安卓调用系统相机拍照,并把图片保存到SD卡中 进行 尺寸和质量的压缩
- /res/drawable图片保存到sd卡
- android保存Bitmap图片到sd卡
- android保存图片到本地sd卡
- View 截取保存图片到sd卡
- 安卓:启动service,下载网络图片,并将图片存放到内存卡,保存成功后发出广播提醒,然后从SD卡读出显示
- 安卓adb-截图/录屏命令,保存到SD卡,导出到本地
- 数据信息保存到SD卡使用流方式
- Android图片操作-保存图片到SD卡
- Android图片操作-保存图片到SD卡
- Android图片操作-保存图片到SD卡
- 安卓保存图片到相册
- 安卓SD卡读写、String和byte转换
- Android有进度条异步任务下载图片
- 开启 Bitnami Redmine 中 phpmyadmin的远程登录权限
- 自定义常用控件之~带placeholder的textview
- 改进你的网页
- 二维数组寻找鞍点
- 安卓保存图片到SD卡,使用byte流
- 1、欢迎使用Swift来编写iOS项目
- 中文标点符号unicode码
- bzoj 1087 题解
- guava-IO使用
- 第一、二章
- 可执行文件的装载与执行
- 网页开发的6种在线调试环境
- 转载大神IOS开发系列【8】--UITableView全面解析