繁体   English   中英

我已经以相同的意图从图库中获取图像到图像视图中,然后在图像视图上单击我要通过意图向该图像发送另一个活动。

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM