欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程使用Intent傳遞圖片的方法詳解

 更新時(shí)間:2017年02月27日 10:28:22   作者:Jacob-wj  
這篇文章主要介紹了Android編程使用Intent傳遞圖片的方法,結(jié)合實(shí)例形式分析了Android基于Intent傳輸圖片的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(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);

接收圖片:

//接收的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é)

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論