Bitmap转为本地URL并存在Card && 本地URL转Bitmap

附各种转换:
http://glblong.blog.51cto.com/3058613/1304090

这里要申明权限:
 
    
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

bitmap转本地URL:
 
    
  1. public static String getSDPath() {
  2. File sdDir = null;
  3. boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
  4. // 判断sd卡是否存在
  5. if (sdCardExist) {
  6. sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
  7. }
  8. return sdDir.toString();
  9. }
  10. /**
  11. * 保存文件
  12. *
  13. * @param bm
  14. * @param fileName
  15. * @throws IOException
  16. */
  17. public void saveFile(Bitmap bm, String fileName) throws IOException {
  18. //获得路径
  19. String path = getSDPath() + "/revoeye/";
  20. File dirFile = new File(path);
  21. if (!dirFile.exists()) {
  22. dirFile.mkdirs();//保存路径
  23. }
  24. // 这里最好不要保存的名字都一样,容易出现imageLoader缓存的问题
  25. //path + fileName + ".jpg" 就是本地的URL
  26. File myCaptureFile = new File(path + fileName + ".jpg");
  27. if(myCaptureFile.exists()){
  28. myCaptureFile.delete();
  29. }
  30. myCaptureFile.createNewFile();
  31. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
  32. bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
  33. bos.flush();
  34. bos.close();
  35. }

本地URL转Bitmap:
 
    
  1. /**缩小图片到width和height的范围内
  2. *
  3. * @param path 图片的位置
  4. * @param width 宽度
  5. * @param height 高度
  6. * @return
  7. */
  8. public static Bitmap resizePhoto(String path, int width, int height) {
  9. BufferedInputStream in = null;
  10. try {
  11. in = new BufferedInputStream(new FileInputStream(new File(path)));
  12. BitmapFactory.Options options = new BitmapFactory.Options();
  13. options.inJustDecodeBounds = true;
  14. BitmapFactory.decodeStream(in, null, options);
  15. int i = 0;
  16. Bitmap bitmap = null;
  17. while (true) {
  18. if ((options.outWidth >> i <= width) && (options.outHeight >> i <= height)) {
  19. in = new BufferedInputStream(new FileInputStream(new File(path)));
  20. options.inSampleSize = (int) Math.pow(2.0D, i);
  21. options.inJustDecodeBounds = false;
  22. bitmap = BitmapFactory.decodeStream(in, null, options);
  23. break;
  24. }
  25. i += 1;
  26. }
  27. return bitmap;
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. } finally {
  31. try {
  32. if (in != null) {
  33. in.close();
  34. }
  35. } catch (Exception e) {
  36. // ignore
  37. }
  38. }
  39. return null;
  40. }








转载于:https://www.cnblogs.com/fruitbolgs/p/4705567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值