Android Studio——Android Bitmap开发之旅--基本操作
2016-01-15 17:38
495 查看
原文链接:http://blog.csdn.net/weihan1314/article/details/8012283
在介绍Bitmap--OOM 异常时,首先介绍一下Bitmap有哪几种加载方式。通常Bitmap的加载方式有Resource资源加载、本地(SDcard)加载、网络加载等加载方式。
Assets资源加载方式:
[java] view
plaincopy
AssetManager am = getAssets();
InputStream is = am.open("high_pixel_img.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is);
Res资源加载方式:
[java] view
plaincopy
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
[java] view
plaincopy
String file_name = Environment.getExternalStorageDirectory().toString()+"/"+"high_pixel_img.jpg";
nbsp;Bitmap bitmap = BitmapFactory.decodeFile(file_name);
注意:网络加载图片的时候必须在非主线成中操作
[java] view
plaincopy
String website = "http://www.baidu.com/img/baidu_sylogo1.gif";
URL image_url = new URL(website);
HttpURLConnection conn = (HttpURLConnection) image_url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
2 Bitmap | Drawable | InputStream | Byte[ ] 之间进行转换
[java] view
plaincopy
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
[java] view
plaincopy
Drawable drawable = new BitmapDrawable(bitmap);
[java] view
plaincopy
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] info = baos.toByteArray();
[java] view
plaincopy
Bitmap bitmap = BitmapFactory.decodeByteArray(byte, 0, b.length);
[java] view
plaincopy
InputStream is = getResources().openRawResource(id);
Bitmap bitmap = BitmaoFactory.decodeStream(is);
[java] view
plaincopy
InputStream is = getResources().openRawResource(id);//也可以通过其他方式接收一个InputStream对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024*2];
int len = 0;
while ((len = is.read(b, 0, b.length)) != -1)
{
baos.write(b, 0, len);
baos.flush();
}
byte[] bytes = baos.toByteArray();
3 转换Bitmap大小
[java] view
plaincopy
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
Bitmap target = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(tartget);
canvas.scale(scale,scale);
Paint paint = new Pain(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap,0,0,paint);
bitmap.recycle();
plaincopy
String filename = "save.jpg";
File file = new File(Environmnet.getExternalStorageDirectory,filename);
try{
OutputStream os = new FileOutputString(file);
bitmap.compress(CompressFormat.JPEG,100,os);
}catch(FileNotFoundException e){
e.printStackTrace();
}
1 Bitmap加载方式
在介绍Bitmap--OOM 异常时,首先介绍一下Bitmap有哪几种加载方式。通常Bitmap的加载方式有Resource资源加载、本地(SDcard)加载、网络加载等加载方式。
1.1 Resource资源加载
Assets资源加载方式:[java] view
plaincopy
AssetManager am = getAssets();
InputStream is = am.open("high_pixel_img.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is);
Res资源加载方式:
[java] view
plaincopy
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
1.2 本地(SDcard)加载
[java] viewplaincopy
String file_name = Environment.getExternalStorageDirectory().toString()+"/"+"high_pixel_img.jpg";
nbsp;Bitmap bitmap = BitmapFactory.decodeFile(file_name);
文件描述符
1.3 网络加载
注意:网络加载图片的时候必须在非主线成中操作[java] view
plaincopy
String website = "http://www.baidu.com/img/baidu_sylogo1.gif";
URL image_url = new URL(website);
HttpURLConnection conn = (HttpURLConnection) image_url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
2 Bitmap | Drawable | InputStream | Byte[ ] 之间进行转换
2.1 Drawable转化成Bitmap
[java] viewplaincopy
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
2. 2 Bitmap转换成Drawable
[java] viewplaincopy
Drawable drawable = new BitmapDrawable(bitmap);
2.3 Bitmap转换成byte[]
[java] viewplaincopy
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] info = baos.toByteArray();
2.4 byte[]转换成Bitmap
[java] viewplaincopy
Bitmap bitmap = BitmapFactory.decodeByteArray(byte, 0, b.length);
2.5 InputStream转换成Bitmap
[java] viewplaincopy
InputStream is = getResources().openRawResource(id);
Bitmap bitmap = BitmaoFactory.decodeStream(is);
2.6 InputStream转换成byte[]
[java] viewplaincopy
InputStream is = getResources().openRawResource(id);//也可以通过其他方式接收一个InputStream对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024*2];
int len = 0;
while ((len = is.read(b, 0, b.length)) != -1)
{
baos.write(b, 0, len);
baos.flush();
}
byte[] bytes = baos.toByteArray();
3 转换Bitmap大小
[java] viewplaincopy
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
Bitmap target = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(tartget);
canvas.scale(scale,scale);
Paint paint = new Pain(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap,0,0,paint);
bitmap.recycle();
4 将Bitmap保存为本地文件
[java] viewplaincopy
String filename = "save.jpg";
File file = new File(Environmnet.getExternalStorageDirectory,filename);
try{
OutputStream os = new FileOutputString(file);
bitmap.compress(CompressFormat.JPEG,100,os);
}catch(FileNotFoundException e){
e.printStackTrace();
}
相关文章推荐
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (上)
- Android开发笔记(五十)定时器AlarmManager
- Android_GreenDao傻瓜式入门
- Android中的同步机制
- Android Studio——位图BitMap
- 第2部分 创建第一个Android程序
- Android-PullToRefresh库中的PullToRefreshListView添加header
- Android Studio中关于getter和setter的生成
- Android——按钮操作
- Android Studio安装插件
- 实例解析Android ImageView的scaleType属性
- 王立平--android中的anim(动画)
- 详解Android MVP开发模式
- Android 讲解:广播
- Android程序开发之动态设置ImageView的亮度
- 自动生成Android屏幕适配的dimens.xml文件工具类
- Android 获取手机图片保存的一个通用工具类
- Android Library Project工程导出jar包供其它工程使用
- Android内存泄露分析之StrictMode
- Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发环境