Android Bitmap开发之旅--基本操作
2012-09-24 12:15
423 查看
1 Bitmap加载方式
在介绍Bitmap--OOM 异常时,首先介绍一下Bitmap有哪几种加载方式。通常Bitmap的加载方式有Resource资源加载、本地(SDcard)加载、网络加载等加载方式。1.1 Resource资源加载
Assets资源加载方式:AssetManager am = getAssets(); InputStream is = am.open("high_pixel_img.jpg"); Bitmap bitmap = BitmapFactory.decodeStream(is);
Res资源加载方式:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);
1.2 本地(SDcard)加载
String file_name = Environment.getExternalStorageDirectory().toString()+"/"+"high_pixel_img.jpg"; Bitmap bitmap = BitmapFactory.decodeFile(file_name);
文件描述符
1.3 网络加载
注意:网络加载图片的时候必须在非主线成中操作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
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
Drawable drawable = new BitmapDrawable(bitmap);
2.3 Bitmap转换成byte[]
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
Bitmap bitmap = BitmapFactory.decodeByteArray(byte, 0, b.length);
2.5 InputStream转换成Bitmap
InputStream is = getResources().openRawResource(id); Bitmap bitmap = BitmaoFactory.decodeStream(is);
2.6 InputStream转换成byte[]
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大小
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保存为本地文件
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(); }
以上是关于Bitmap的相关操作,如果大家在阅读中发现有什么问题,请在评论中留言。
相关文章推荐
- Android Bitmap开发之旅--基本操作
- Android Studio——Android Bitmap开发之旅--基本操作
- Android Bitmap开发之基本操作
- android开发笔记之db数据库基本操作
- JNI及Android JNI 开发基本知识和具体操作步骤
- Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas
- Android手机蓝牙的开发——(一)蓝牙的开启关闭配对等基本操作
- Android开发-对bitmap的一些的操作
- JNI及Android JNI 开发基本知识和具体操作步骤
- android开发之蓝牙的基本操作
- Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas
- 最新基于高德地图的android进阶开发(5)地图的基本操作、事件监听、用户UI、图层选择等
- Android游戏开发基础part4--Bitmap位图的渲染与操作
- Bitmap位图渲染与操作(图片移动,放大,缩小,旋转,镜像发转)--android游戏开发
- Android开发笔记(五十五)手机设备基本操作
- JNI及Android JNI 开发基本知识和具体操作步骤
- Android游戏开发基础part4--Bitmap位图的渲染与操作
- android开发 基本操作
- Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas
- Android开发笔记(十四)数据库操作的基本方法