Android编程使用Intent传递图片的方法详解
程序员文章站
2023-12-13 20:40:40
本文实例讲述了android编程使用intent传递图片的方法。分享给大家供大家参考,具体如下:
基本思路是先把bitmap转化为byte数组,用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程序设计有所帮助。