当前位置:   article > 正文

Android 图片Bitmap保存到内存卡_android 存储bitmap到sd卡 实现

android 存储bitmap到sd卡 实现

一、什么是Android中的Bitmap

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。

二、什么是内存卡

SD卡存储卡,是用于手机、数码相机、便携式电脑、MP3和其他数码产品上的独立存储介质,一般是卡片的形态,故统称为“存储卡”,又称为“数码存储卡”、“数字存储卡”、“储存卡”等。

三、图片为什么要存在内存卡里

部分手机的手机内存是不够的,一个程序如果产生的图片量太大,很容易占用了宝贵的手机内存,所以安全起见,图片最好存放在内存卡中

四、如何存放

  1. import java.io.BufferedInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.os.Environment;
  12. import android.os.StatFs;
  13. import android.util.Log;
  14. public class BitmaptoCard {
  15. private static int FREE_SD_SPACE_NEEDED_TO_CACHE = 1;
  16. private static int MB = 1024 * 1024;
  17. /**
  18. * 保存Bitmap到sdcard
  19. *
  20. * @param dir
  21. * @param bm
  22. * @param filename
  23. * @param quantity
  24. */
  25. public static boolean saveBmpToSd(String dir, Bitmap bm, String filename,
  26. int quantity, boolean recyle) {
  27. boolean ret = true;
  28. if (bm == null) {
  29. return false;
  30. }
  31. if (FREE_SD_SPACE_NEEDED_TO_CACHE > freeSpaceOnSd()) {
  32. bm.recycle();
  33. bm = null;
  34. return false;
  35. }
  36. File dirPath = new File(dir);
  37. if (!exists(dir)) {
  38. dirPath.mkdirs();
  39. }
  40. if (!dir.endsWith(File.separator)) {
  41. dir += File.separator;
  42. }
  43. File file = new File(dir + filename);
  44. OutputStream outStream = null;
  45. try {
  46. file.createNewFile();
  47. outStream = new FileOutputStream(file);
  48. bm.compress(Bitmap.CompressFormat.JPEG, quantity, outStream);
  49. outStream.flush();
  50. outStream.close();
  51. } catch (FileNotFoundException e) {
  52. e.printStackTrace();
  53. ret = false;
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. ret = false;
  57. } catch (OutOfMemoryError e) {
  58. e.printStackTrace();
  59. ret = false;
  60. } finally {
  61. if (outStream != null) {
  62. try {
  63. outStream.close();
  64. } catch (IOException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. }
  68. }
  69. if (recyle && !bm.isRecycled()) {
  70. bm.recycle();
  71. bm = null;
  72. Log.e("BitmaptoCard", "saveBmpToSd, recyle");
  73. }
  74. }
  75. return ret;
  76. }
  77. /**
  78. * 保存Bitmap到sdcard
  79. *
  80. * @param dir
  81. * @param bm
  82. * @param filename
  83. * @param quantity
  84. */
  85. public static boolean saveBmpToSd(String dir, Bitmap bm, String filename,
  86. int quantity) {
  87. return saveBmpToSd(dir, bm, filename, quantity, false);
  88. }
  89. /**
  90. * 保存Bitmap到sdcard
  91. *
  92. * @param dir
  93. * @param bm
  94. * @param filename
  95. * @param quantity
  96. */
  97. public static boolean saveBmpToSd(String dir, String srcFile,
  98. String filename, int quantity) {
  99. if (srcFile == null) {
  100. return false;
  101. }
  102. Bitmap bmp = BitmapFactory.decodeFile(srcFile);
  103. return saveBmpToSd(dir, bmp, filename, quantity);
  104. }
  105. /**
  106. * 保存Bitmap到sdcard
  107. *
  108. * @param dir
  109. * @param bm
  110. * @param filename
  111. * @param quantity
  112. */
  113. public static boolean saveBmpToSd(String dir, String srcFile,
  114. String filename, int quantity, boolean recyle) {
  115. if (srcFile == null) {
  116. return false;
  117. }
  118. Bitmap bmp = BitmapFactory.decodeFile(srcFile);
  119. return saveBmpToSd(dir, bmp, filename, quantity, recyle);
  120. }
  121. /**
  122. * 保存Bitmap到sdcard
  123. *
  124. * @param dir
  125. * @param bm
  126. * @param filename
  127. * @param quantity
  128. */
  129. public static boolean saveBmpToSd(String dir, String srcFile,
  130. String filename, int quantity, float size, boolean recyle) {
  131. if (srcFile == null) {
  132. return false;
  133. }
  134. Bitmap bmp = convertToThumb(readFileToBuffer(srcFile), size);
  135. return saveBmpToSd(dir, bmp, filename, quantity, recyle);
  136. }
  137. /**
  138. * 保存Bitmap到sdcard
  139. *
  140. * @param dir
  141. * @param bm
  142. * @param filename
  143. * @param quantity
  144. */
  145. public static boolean saveBmpToSd(String dir, String srcFile,
  146. String filename, int quantity, float size) {
  147. if (srcFile == null) {
  148. return false;
  149. }
  150. Bitmap bmp = convertToThumb(readFileToBuffer(srcFile), size);
  151. return saveBmpToSd(dir, bmp, filename, quantity);
  152. }
  153. /**
  154. * 保存Bitmap到sdcard
  155. *
  156. * @param dir
  157. * @param bm
  158. * @param filename
  159. * @param quantity
  160. */
  161. public static boolean saveBmpToSd(String dir, Bitmap bmp, String filename,
  162. int quantity, float size) {
  163. if (bmp == null) {
  164. return false;
  165. }
  166. bmp = convertToThumb(readBitmap(bmp), size);
  167. return saveBmpToSd(dir, bmp, filename, quantity);
  168. }
  169. /**
  170. * 获取sdcard路径
  171. *
  172. * @return
  173. */
  174. public static String getSdcardPath() {
  175. return Environment.getExternalStorageDirectory().getPath()
  176. + File.separator;
  177. }
  178. /**
  179. * 验证文件是否存在
  180. *
  181. * @param url
  182. * @return
  183. */
  184. public static boolean exists(String url) {
  185. File file = new File(url);
  186. return file.exists();
  187. }
  188. /**
  189. * 检测sdcard可用空间
  190. *
  191. * @return
  192. */
  193. public static int freeSpaceOnSd() {
  194. StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
  195. .getPath());
  196. double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat
  197. .getBlockSize()) / MB;
  198. return (int) sdFreeMB;
  199. }
  200. /**
  201. * Bitmap --> byte[]
  202. *
  203. * @param bmp
  204. * @return
  205. */
  206. private static byte[] readBitmap(Bitmap bmp) {
  207. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  208. bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos);
  209. try {
  210. baos.flush();
  211. baos.close();
  212. } catch (IOException e) {
  213. e.printStackTrace();
  214. }
  215. return baos.toByteArray();
  216. }
  217. /**
  218. * 保存Bitmap到sdcard
  219. *
  220. * @param dir
  221. * @param bm
  222. * @param filename
  223. * @param quantity
  224. */
  225. public static boolean saveBmpToSd(String filePath, Bitmap bm, int quantity) {
  226. if (filePath == null) {
  227. return false;
  228. }
  229. int end = filePath.lastIndexOf(File.separator);
  230. String dir = filePath.substring(0, end);
  231. String filename = filePath.substring(end);
  232. return saveBmpToSd(dir, bm, filename, quantity);
  233. }
  234. /**
  235. * @description: 通过文件路径将对应文件转为byte[]
  236. * @param fileName
  237. * @return
  238. */
  239. public static byte[] getByte(String fileName) {
  240. if (fileName == null || "".equals(fileName)) {
  241. return new byte[0];
  242. }
  243. File file = new File(fileName);
  244. if (file.exists()) {
  245. try {
  246. FileInputStream fin = new FileInputStream(fileName);
  247. int length = fin.available();
  248. byte[] buffer = new byte[length];
  249. fin.read(buffer);
  250. // res = EncodingUtils.getString(buffer, "UTF-8");
  251. fin.close();
  252. return buffer;
  253. } catch (Exception e) {
  254. Log.e("BitmaptoCard", "getByte fail:" + fileName);
  255. return new byte[0];
  256. }
  257. } else {
  258. Log.e("BitmaptoCard", "getByte file no exists :" + fileName);
  259. return new byte[0];
  260. }
  261. }
  262. /**
  263. * 将图片、语音或者文件读入到byte缓冲数组
  264. *
  265. * @param filePath
  266. * @return
  267. */
  268. public static byte[] readFileToBuffer(String filePath) {
  269. if (filePath == null || filePath.trim().equals("")) {
  270. Log.e("BitmaptoCard", "readFileToBuffer, path is null:" + filePath);
  271. return null;
  272. }
  273. File file = new File(filePath);
  274. if (!file.exists()) {
  275. Log.e("BitmaptoCard", "readFileToBuffer, file is not exists:"
  276. + filePath);
  277. return null;
  278. }
  279. byte[] buffer = new byte[(int) file.length()];
  280. FileInputStream fis = null;
  281. try {
  282. fis = new FileInputStream(file);
  283. BufferedInputStream bis = new BufferedInputStream(fis);
  284. bis.read(buffer);
  285. bis.close();
  286. } catch (Exception e) {
  287. e.printStackTrace();
  288. }
  289. return buffer;
  290. }
  291. /**
  292. * 检查图片是否超过一定值,是则缩小
  293. *
  294. * @param view
  295. * @param strFileName
  296. */
  297. public static Bitmap convertToThumb(byte[] buffer, float size) {
  298. // 获取原图宽度
  299. BitmapFactory.Options options = new BitmapFactory.Options();
  300. options.inJustDecodeBounds = true;
  301. options.inPurgeable = true;
  302. options.inInputShareable = true;
  303. Bitmap bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,
  304. options);
  305. // 计算缩放比例
  306. float reSize = options.outWidth / size;
  307. if (options.outWidth > options.outHeight) {
  308. reSize = options.outHeight / size;
  309. }
  310. if (reSize <= 0) {
  311. reSize = 1;
  312. }
  313. Log.d("BitmaptoCard", "convertToThumb, reSize:" + reSize);
  314. // 缩放
  315. options.inJustDecodeBounds = false;
  316. options.inSampleSize = (int) reSize;
  317. if (bm != null && !bm.isRecycled()) {
  318. bm.recycle();
  319. bm = null;
  320. Log.e("BitmaptoCard", "convertToThumb, recyle");
  321. }
  322. bm = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
  323. if (bm == null) {
  324. Log.e("BitmaptoCard", "convertToThumb, decode fail:" + null);
  325. return null;
  326. }
  327. return bm;
  328. }
  329. }

示例代码:http://download.csdn.net/detail/stop_pig/7162695

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/303704
推荐阅读
相关标签
  

闽ICP备14008679号