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);
return bitmap;
}
2、從資源中獲取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4、 byte[] → Bitmap
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
String txt = edInput.getText().toString(); Pattern p = Pattern.compile("[0-9]*"); |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
// 豎屏
}
else if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//橫屏
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Android的Btimap處理大圖片解決方法
原文: http://www.android123.com.cn/kaifafaq/594.html
我們都知道Android的Dalvik VM爲一個應用提供了大約16MB的內存,一般我們處理超過8MB的圖片將會出現OutOfMemoryError異常,我們解碼一個圖片爲了防止內存不 足的異常我們可以使用BitmapFactory.Options 的udeinTempStorage屬性解決,代碼如下:
BitmapFactory.Options cwj = new BitmapFactory.Options();
cwj.inTempStorage = new byte[1024*1024*5]; //5MB的臨時存儲空間
Bitmap bm = BitmapFactory.decodeFile(inputStream,cwj); //這裏cwj爲Options屬性