简体   繁体   中英

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.

Code :

 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);
        }
    });

Use a Static HashMap to store the Images. On Image click just put the image with its name in your HashMap and you can get the images wherever you want just by its name.

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 : just check for image by its name in HashMap .

       Intent intent = getIntent();
       String s = intent.getStringExtra("ImageName");   
       if (globalImageMap.containsKey(s)) {  
                yourImageView.setImageBitmap(globalImageMap.get(s));
}

You need to convert the Bitmap to a byte[] then you can convert the byte[] to the Bitmap again in your other activity..

Bitmap to 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[] to Bitmap

byte[] ba= getIntent().getByteArrayExtra("bpm_image");
Bitmap bmp = BitmapFactory.decodeByteArray(ba, 0, ba.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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