读取sd卡下图片,由图片路径转换为bitmap
来源:互联网 发布:cpa培训班 知乎 编辑:程序博客网 时间:2024/05/24 15:38
01 public Bitmap convertToBitmap(String path, int w, int h) {
02 BitmapFactory.Options opts = new BitmapFactory.Options();
03 // 设置为ture只获取图片大小
04 opts.inJustDecodeBounds = true;
05 opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
06 // 返回为空
07 BitmapFactory.decodeFile(path, opts);
08 int width = opts.outWidth;
09 int height = opts.outHeight;
10 float scaleWidth = 0.f, scaleHeight = 0.f;
11 if (width > w || height > h) {
12 // 缩放
13 scaleWidth = ((float) width) / w;
14 scaleHeight = ((float) height) / h;
15 }
16 opts.inJustDecodeBounds = false;
17 float scale = Math.max(scaleWidth, scaleHeight);
18 opts.inSampleSize = (int)scale;
19 WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
20 return Bitmap.createScaledBitmap(weak.get(), w, h, true);
21 }
其中w和h你需要转换的大小
path转换为bitmap:上面方法即可;
imageview获取drawable并转换为 bitmap :Bitmap bt= ((BitmapDrawable) mImageview.getDrawable()).getBitmap();
resourceid转换为bitmap:Bitmap bt = BitmapFactory.decodeResource(getResources(), R.drawable.resourceid);
Drawable转换为bitmap:Bitmap bt= ((BitmapDrawable) Drawable).getBitmap();
因为BitmapDrawable是继承Drawable,所以可以灵活的转换
- 读取sd卡下图片,由图片路径转换为bitmap
- 读取sd卡下图片,由图片路径转换为bitmap
- 读取sd卡下图片,由图片路径转换为bitmap
- 读取sd卡下图片,由图片路径转换为bitmap
- 解决 读取sd卡图片,判断Bitmap 为null
- Android获取SD卡根目录下图片
- 读取SD卡图片 转化Bitmap
- 读取本地(SD卡)图片转为Bitmap
- 读取SD卡图片 转化Bitmap
- Java中关于如何读取不同路径下图片/文件
- 网络图片转为bitmap 转换为文件(File)保存至SD卡中
- 获取SD卡根目录下图片的方法
- android 读取本地或网络图片,转换为Bitmap -
- android 读取本地或网络图片,转换为Bitmap -
- 使用Bitmap遍历Assets目录下图片
- Android将res路径下图缓存到sd卡
- bitmap写入sd卡,从sd卡读取bitmap
- 图片资源类型转换为bitmap
- [LeetCode]58.Length of Last Word
- 手机卡,SIM卡,USIM 卡,nano卡
- sql拼接与绑定变量
- Java Basic2(java 中的相等概念简单介绍)
- MediaPlayer的简单使用
- 读取sd卡下图片,由图片路径转换为bitmap
- 一种给Map赋值的优雅方式
- Ubuntu新装adt-bundle中Android工程无法自动生成R文件的解决办法
- UNIX环境C语言编程(12)-进程间通信
- Java条件语句之嵌套 if
- Java内存分析
- 云计算运营模式实例共享
- iOS 音频本地和在线播放器
- Linux新建和增加Swap分区