1

I have an application that allow users to choose picture from native gallery then I show this image in image view widget.

My question is: 1-i have to send this image to another Activity. How can i do it.

2-in the receiver Activity i should show it in image view widget as in image not link or Something

I tried this code but it gives me a RunTime Error

   Bitmap image = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.RGB_565);
   view.draw(new Canvas(image));
   String url = Images.Media.insertImage(getContentResolver(), image,"title", null);
Igor
  • 30,885
  • 14
  • 70
  • 107
Maha
  • 457
  • 1
  • 6
  • 21

4 Answers4

0

Maybe this is not what you're looking for and it's a bit poor but saved my life when I needed to pass objects between Activities.

    public class MagatzemImg {
         private static MagatzemImg instance = null;
         private static Bitmap img;

         public MagatzemImg(){ 
            img=null; 
         }

         public static MagatzemImg getInstance() {
            if (instance == null)
            instance = new MagatzemImg();
            return instance;
         }

         public static void setImg(Bitmap im){ 
            img = im; 
         }
         public static Bitmap getImg(){
             Bitmap imgAux = img;
             img = null;
             return imgAux;
         }
    }

And then from the new activity:

    MagatzemImg.getInstance();
    image = MagatzemImg.getImg();

You can 'assure' to the new Activity that the image exists inside the Static Class through putExtra("image",true) or something else you prefer, like checking if the "image" is null.

Jordi
  • 566
  • 2
  • 8
  • 16
0

Use intent putExtra and send uri of the image user selected in Acvtivity1 and in second activity use intent getExtra to read the uri

Refer this answer https://stackoverflow.com/a/7325248/308251

Community
  • 1
  • 1
Sandeep Manne
  • 5,640
  • 5
  • 36
  • 51
  • ok this is how can i send a normal value but what about the image?i think i should doing some transfer to Bitmap then convert it to normal image to show it in second Activity,please give me the code to doing this step – Maha Apr 03 '12 at 16:58
  • but why you need to send bitmap, you can send uri of selected bitmap it is more optimized, is there any usecase why you prefer to send bitmap? – Sandeep Manne Apr 03 '12 at 17:01
0

Yes you can pass in putExtra method.

Intent i = new Intent(this, Second.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("Arraybyte", bs.toByteArray());
startActivity(i);

And In Second.java

if(getIntent().hasExtra("Arraybyte")) {
ImageView iv= new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);        
iv.setImageBitmap(b);

}

Bhavin
  • 5,572
  • 4
  • 21
  • 26
0

Just follow the below steps...

Uri uri = null; 1) on any click event use the below code to open native gallery

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),0);

This will open gallery select picture will return you to your activity. OnActivity result.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
    if (resultCode == RESULT_OK) {
    try {
    uri = Uri.parse(data.getDataString());
    imageView.setImageUri(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
    }}
    break;
    }
}

2) Also instead of passing the image you can pass the URI to next activity as you pass string and inthe secont activity you get it using intent.

Intent i = new Intent(this, Second.class);
i.putExtra("URI", uri.toString() );
startActivity(i);

and in the second activity

String uri = getIntent().getStringExtra("URI");

Now you have string just set it to the image view like below

imageView.setImageUri(Uri.parse(uri));
Shankar Agarwal
  • 39,320
  • 8
  • 68
  • 66
  • i already did the gallery part but i need to send it to another activity and show it as image. can you please give the code that doing this step. – Maha Apr 03 '12 at 16:55