Android中傳遞圖片的3種方法

方法零:java

網上有人想到一種方法,就是先把圖片變小,再傳遞,最後在接收端把圖片放大。。這種方法或許可行,可是我認爲這很扯,因此無視!數組

方法一:

基本思路是先把bitmap轉化爲byte數組,用Intent傳遞數組,在將數組轉化爲bitmap
 
bitmap轉化爲byte數組的方法:
 this

private byte[] Bitmap2Bytes(Bitmap bm){       
    ByteArrayOutputStream baos = new ByteArrayOutputStream();         
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);         
    return baos.toByteArray();       
   }   

   
byte數組轉化爲bitmap方法:spa

byte buff[]=mIntent.getByteArrayExtra("image");    
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);   


方法2:

發送圖片:  .net

Intent intent  = new Intent(ChangePortraitActivity.this , UserProfileActivity.class);    
 mImageView.setDrawingCacheEnabled(Boolean.TRUE);    
intent.putExtra("BITMAP", mImageView.getDrawingCache()); //這裏能夠放一個bitmap    
 startActivity(intent);  


接收圖片:code

//接收的activity  
Intent intent = getIntent();    
if (intent  != null &&  intent.getParcelableExtra("BITMAP") != null) {    
    Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BITMAP");    
    mImageViewPortrait.setImageBitmap(bitmap);    
}

參考資料:http://www.jb51.net/article/64795.htmorm

相關文章
相關標籤/搜索