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相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
相关文章
Android中HttpURLConnection类使用介绍
早些时候其实我们都习惯性使用HttpClient,但是后来Android6.0之后不再支持HttpClient,需要添加Apache的jar才行,所以,就有很多开发者放弃使用HttpClient了,HttpURLConnection毕竟是标准Java接口(java.net) ,适配性还是很强的2022-12-12
Android中使用GridView实现仿微信图片上传功能(附源代码)
由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传、拍照、本地选择、相片裁剪等功能,如果有需要的朋友可以看一下2017-08-08
android如何设置小区广播默认信道(50与60并支持双卡)
置小区广播默认信道50与60,并支持双卡主要是印度市场,具体的实现如下,感兴趣的朋友可以参考下哈2013-06-06
Android WindowManager深层理解view绘制实现流程
WindowManager是Android中一个重要的Service,是全局且唯一的。WindowManager继承自ViewManager。WindowManager主要用来管理窗口的一些状态、属性、view增加、删除、更新、窗口顺序、消息收集和处理等2022-11-11
Android Studio 合并module到统一文件夹的方法
这篇文章主要介绍了Android Studio 合并module到统一文件夹的方法,补充介绍了android studio关于同名资源文件的合并技巧,需要的朋友可以参考下2018-04-04


最新评论