用Intent将图片发送到另外一个Activity(用bitmap)
2017-12-29 01:08
330 查看
首先:我们需求确定的思路是,用Intent将图片发送到另外一个activity,,第一个页面先将bitmap位图转化成byte字节数组,然后发送到第二个activity页面,第二个页面将byte数组再转化成bitmap位图数组,即可在第二个页面显示出发送过来的图片:话不多说,上代码图:
第一个页面的JAVA代码:(自己把包导入进去)
public class MainActivity extends Activity implements OnClickListener {
private Bitmap bitmap;
byte buff[] = new byte[125*250];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImageView = (ImageView) findViewById(R.id.image);
Button bt1 = (Button) findViewById(R.id.bt1);
bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.accept);
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, seconde.class);
startActivity(mIntent);
}
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
}
第一个页面的XML代码:里面就一个ImageView,和Button
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="50dp"
android:text="Button" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"/>
<--imageview注意这里不要添加src或者background,否则会发送两个图片过去-->
第二个页面的JAVA代码:
public class seconde extends Activity{
private Bitmap bitmap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
}
}
第二个页面的XML代码:
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
记住:因为第二个second类是一个Activity,所以需要在androidManifest注册activity,否则会闪退,报错:
完毕,够详细了吧~~
第一个页面的JAVA代码:(自己把包导入进去)
public class MainActivity extends Activity implements OnClickListener {
private Bitmap bitmap;
byte buff[] = new byte[125*250];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mImageView = (ImageView) findViewById(R.id.image);
Button bt1 = (Button) findViewById(R.id.bt1);
bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.accept);
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, seconde.class);
startActivity(mIntent);
}
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
}
第一个页面的XML代码:里面就一个ImageView,和Button
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="50dp"
android:text="Button" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"/>
<--imageview注意这里不要添加src或者background,否则会发送两个图片过去-->
第二个页面的JAVA代码:
public class seconde extends Activity{
private Bitmap bitmap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
}
}
第二个页面的XML代码:
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
记住:因为第二个second类是一个Activity,所以需要在androidManifest注册activity,否则会闪退,报错:
完毕,够详细了吧~~
相关文章推荐
- android 计算Bitmap占byte大小,以及Bitmap中像素数的计算,另外再给出一个按尺寸裁剪图片的方法
- 【第三篇】学习 android 事件总线androidEventbus之list数据事件的传递,发送list数据事件到另外一个Activity
- Android学习笔记(九)—— 调用另外一个Activity — Intent对象的使用
- 如何在非Activity类中,跳转到另外一个Activity类(context.startActivity(intent); )
- android中,如何用intent去启动另外一个activity
- 使用intent由一个Activity启动另外一个Activity
- 利用Intent启动另外一个Activity的方法
- Intent系列1——启动另外一个Activity
- Intent传递数据到另外一个Activity
- 如何实现点击GridView中一个图片后,如何传递至一个新的Activity中的imageview里面显示?
- 一个程序A通过intent调用程序B的Activity
- Android Activity往另外一个Activity传递数据,Fragment从另一个Activity获取数据
- 【android基础】之intent传递数组[应用于activity之间传递图片参数等]
- 在一个apk中调用另外一个apk中的activity
- Android培训---启动另外一个Activity
- 启动另外的一个应用程序的Activity(三种方式)
- Android 实现在Activity中操作刷新另外一个Activity数据列表
- 点击一个图片换成另外一个图片 jquery
- Activity往另外一个Activity传值,Fragment获取另外一个Activity里面的值。
- Activity和Intent机制(内含一个原创的例子)