public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); } private static class SampleView extends View { private Bitmap mBitmap; public SampleView(Context context) { super(context); setFocusable(true); Bitmap bmp = null; String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/sx/852.png"; BitmapFactory.Options options=new Options(); options.inPreferredConfig = Bitmap.Config.ARGB_4444; bmp = BitmapFactory.decodeFile(filePath, options); Log.v(TAG,"decode success--threadid=="+Thread.currentThread().getId()+",name=="+Thread.currentThread().getName()); mBitmap = bmp; //decodeStream 流 /* java.io.InputStream is; is = context.getResources().openRawResource(R.drawable.r8521); BitmapFactory.Options opts = new BitmapFactory.Options(); Bitmap bm; opts.inPreferredConfig = Bitmap.Config.ARGB_4444; //opts.inPreferredConfig = Bitmap.Config.ARGB_8888; bm = BitmapFactory.decodeStream(is, null, opts); mBitmap = bm;*/ // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawColor(0xFF000000); canvas.drawBitmap(mBitmap, 0, 0, null); } }
//当你想dump出来decode出来的bitmap数据的时候 String filename = “XXXXX”;
FileOutputStream fos; try { fos = new FileOutputStream(filename); Bitmap b = mBitmap; b.compress(Bitmap.CompressFormat.PNG, 100, fos); //这样文件就保存到fileName目录下面了 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
在AndroidManifest.xml别忘了配置读写权限
此段code完成:
1.把手机中的852.png decode成ARGB4444的bitmap
2.然后把bitmap画出来,看decode之后的效果