本文实例讲述了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程序设计有所帮助。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。