![](/img/trans.png)
[英]How to click image view and send that image in another activity through intent?
[英]I have taken image from gallery to image view in same intent.And on image view click I want to send that image another Activity through intent.
代码:
imgview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,ImageDivision.class);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
i.putExtra("bmp_img", bmp);
startActivity(i);
}
});
使用静态HashMap
存储图像。 在Image上单击,只需将其名称的图像放入HashMap
,就可以通过名称将其HashMap
获取。
public static HashMap<String, Bitmap> globalImageMap;
onImageClick:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
globalImageMap.put(name,bmp);
Intent intent= new Intent(this, ImageDivision.class);
intent.putExtra("ImageName",name);
startActivity(i);
ImageDivision.class
:只需在HashMap
检查图像名称即可。
Intent intent = getIntent();
String s = intent.getStringExtra("ImageName");
if (globalImageMap.containsKey(s)) {
yourImageView.setImageBitmap(globalImageMap.get(s));
}
您需要将位图转换为byte [],然后可以在其他活动中再次将byte []转换为位图。
位图到字节[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] ba = stream.toByteArray();
Intent intent= new Intent(this, ImageDivision.class);
intent.putExtra("bmp_image",ba);
startActivity(i);
byte []到位图
byte[] ba= getIntent().getByteArrayExtra("bpm_image");
Bitmap bmp = BitmapFactory.decodeByteArray(ba, 0, ba.length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.