android位圖在activity之間傳遞的問題
采用傳統的方式,將bitmap轉化為字節數組,利用intent傳遞byte[]。bitmap專為為byte[]的代碼如下:
Intent in = new Intent(TestAnimation.this,CameraDemo.class); Bitmap m = ((BitmapDrawable)(getResources().getDrawable(R.drawable.ic_launcher))).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); m.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] bitmapByte = baos.toByteArray(); in.putExtra("bitmap", bitmapByte); startActivity(in);`
接收方接收到byte數組可以轉化為bitmap,代碼如下:
Intent in = getIntent(); if(in != null){ byte[] bis = in.getByteArrayExtra("bitmap"); System.out.println("-------CameraDemo bis length="+bis.length); Bitmap bitmap = BitmapFactory.decodeByteArray(bis, 0, bis.length); }
最後更新:2017-04-04 07:03:07