Android編程使用Intent傳遞圖片的方法詳解
本文實(shí)例講述了Android編程使用Intent傳遞圖片的方法。分享給大家供大家參考,具體如下:
基本思路是先把bitmap轉(zhuǎn)化為byte數(shù)組,用Intent傳遞數(shù)組,在將數(shù)組轉(zhuǎn)化為bitmap
bitmap轉(zhuǎn)化為byte數(shù)組的方法:
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
byte數(shù)組轉(zhuǎn)化為bitmap方法:
byte buff[]=mIntent.getByteArrayExtra("image");
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
程序?qū)嵗?/p>
第一個(gè)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();
}
}
第二個(gè)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()); //這里可以放一個(gè)bitmap
startActivity(intent);
接收?qǐng)D片:
//接收的activity
Intent intent = getIntent();
if (intent != null && intent.getParcelableExtra("BITMAP") != null) {
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BITMAP");
mImageViewPortrait.setImageBitmap(bitmap);
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android中intent傳遞list或者對(duì)象的方法
- Android系列之Intent傳遞對(duì)象的幾種實(shí)例方法
- Android中Intent傳遞對(duì)象的3種方式詳解
- Android 通過Intent使用Bundle傳遞對(duì)象詳細(xì)介紹
- Android 使用Intent傳遞數(shù)據(jù)的實(shí)現(xiàn)思路與代碼
- Android開發(fā)之利用Intent實(shí)現(xiàn)數(shù)據(jù)傳遞的方法
- 詳解Android中Intent傳遞對(duì)象給Activity的方法
- Android Intent傳遞數(shù)據(jù)大小限制詳解
相關(guān)文章
Android自動(dòng)化測(cè)試處理各種彈窗的操作方法
這篇文章主要介紹了Android自動(dòng)化測(cè)試中如何處理各種彈窗,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
Android實(shí)現(xiàn)動(dòng)態(tài)改變shape.xml中圖形的顏色
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)改變shape.xml中圖形的顏色,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解Flutter Image組件如何處理圖片加載過程中的錯(cuò)誤
在Flutter中,Image組件可以通過監(jiān)聽加載過程中的錯(cuò)誤來處理圖片加載過程中的錯(cuò)誤,本文小編將給大家詳細(xì)介紹了Flutter Image組件是如何處理圖片加載過程中的錯(cuò)誤,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參下2023-10-10
Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Android 自定義底部上拉控件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android 自定義底部上拉控件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
這篇文章介紹了Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼,有需要的朋友可以參考一下2013-10-10
android實(shí)現(xiàn)數(shù)獨(dú)游戲機(jī)器人
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)數(shù)獨(dú)游戲機(jī)器人,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
unity3d發(fā)布apk在android虛擬機(jī)中運(yùn)行的詳細(xì)步驟(unity3d導(dǎo)出android apk)
這篇文章主要介紹了unity3d發(fā)布apk在android虛擬機(jī)中運(yùn)行的詳細(xì)步驟,需要的朋友可以參考下2014-05-05

