Android編程使用Intent傳遞圖片的方法詳解
本文實例講述了Android編程使用Intent傳遞圖片的方法。分享給大家供大家參考,具體如下:
基本思路是先把bitmap轉化為byte數(shù)組,用Intent傳遞數(shù)組,在將數(shù)組轉化為bitmap
bitmap轉化為byte數(shù)組的方法:
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
byte數(shù)組轉化為bitmap方法:
byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
程序實例:
第一個activity:
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SendImageActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Bitmap bitmap;
byte buff[] = new byte[125*250];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView mImageView = (ImageView) findViewById(R.id.image);
Button bt1 = (Button) findViewById(R.id.bt1);
bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.option24);
buff = Bitmap2Bytes(bitmap);
BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
mImageView.setBackgroundDrawable(mBitmapDrawable);
bt1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent mIntent = new Intent();
mIntent.putExtra("image", buff);
mIntent.setClass(this, activity2.class);
startActivity(mIntent);
}
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
}
第二個activity:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class activity2 extends Activity {
private Bitmap bitmap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ImageView mImageView = (ImageView) findViewById(R.id.image2);
Intent mIntent = getIntent();
byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
mImageView.setBackgroundDrawable(mBitmapDrawable);
}
}
發(fā)送圖片:
Intent intent = new Intent(ChangePortraitActivity.this , UserProfileActivity.class);
mImageView.setDrawingCacheEnabled(Boolean.TRUE);
intent.putExtra("BITMAP", mImageView.getDrawingCache()); //這里可以放一個bitmap
startActivity(intent);
接收圖片:
//接收的activity
Intent intent = getIntent();
if (intent != null && intent.getParcelableExtra("BITMAP") != null) {
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BITMAP");
mImageViewPortrait.setImageBitmap(bitmap);
}
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android中HttpURLConnection類使用介紹
早些時候其實我們都習慣性使用HttpClient,但是后來Android6.0之后不再支持HttpClient,需要添加Apache的jar才行,所以,就有很多開發(fā)者放棄使用HttpClient了,HttpURLConnection畢竟是標準Java接口(java.net) ,適配性還是很強的2022-12-12
Android中使用GridView實現(xiàn)仿微信圖片上傳功能(附源代碼)
由于工作要求最近在使用GridView完成圖片的批量上傳功能,我的例子當中包含仿微信圖片上傳、拍照、本地選擇、相片裁剪等功能,如果有需要的朋友可以看一下2017-08-08
android如何設置小區(qū)廣播默認信道(50與60并支持雙卡)
置小區(qū)廣播默認信道50與60,并支持雙卡主要是印度市場,具體的實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android?WindowManager深層理解view繪制實現(xiàn)流程
WindowManager是Android中一個重要的Service,是全局且唯一的。WindowManager繼承自ViewManager。WindowManager主要用來管理窗口的一些狀態(tài)、屬性、view增加、刪除、更新、窗口順序、消息收集和處理等2022-11-11
Android Studio 合并module到統(tǒng)一文件夾的方法
這篇文章主要介紹了Android Studio 合并module到統(tǒng)一文件夾的方法,補充介紹了android studio關于同名資源文件的合并技巧,需要的朋友可以參考下2018-04-04

