亲宝软件园·资讯

展开

Android Intent传递图片 Android编程使用Intent传递图片的方法详解

Jacob-wj 人气:0
想了解Android编程使用Intent传递图片的方法详解的相关内容吗,Jacob-wj在本文为您仔细讲解Android Intent传递图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,Intent,传递图片,下面大家一起来学习吧。

本文实例讲述了Android编程使用Intent传递图片的方法。分享给大家供大家参考,具体如下:

基本思路是先把bitmap转化为byte数组,用Intent传递数组,在将数组转化为bitmap

bitmap转化为byte数组的方法:

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

byte数组转化为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);
  }
}

发送图片:

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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论