Android Studio——Android Bitmap開發之旅--基本操作
阿新 • • 發佈:2019-01-23
原文連結:http://blog.csdn.net/weihan1314/article/details/8012283
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";
- nbsp;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 = newbyte[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();
- }