0

I am trying to make an App that have 2 Activities. Activity1 is a gallery can call Activity2. Activity2 is a camera app that capture images then send these images to Activity1 and Activity1 will show these images in gallery.I can't send images between two Activities.

This code is my gallery in Activity1. I know SerializebleBitmap but in my code how can I put it in the gallery list? Here is my code:

public class HelloGallery extends Activity {
private Gallery gallery;
private ImageView imgView;
private Button btn;
private static final int SHOW_SUB_FORM = 0;
private String[] imglist;
Bundle extras = getIntent().getExtras();
private Integer[] mImageIds = {
        R.drawable.sample_0,
        R.drawable.sample_1,
        R.drawable.sample_2,
        R.drawable.sample_3,
        R.drawable.sample_4,
        R.drawable.sample_5,
        R.drawable.sample_6,
        R.drawable.sample_7
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));
   imgView = (ImageView)findViewById(R.id.ImageView01); 
    imgView.setImageResource(mImageIds[0]);

    btn = (Button) findViewById(R.id.Takept);


       btn.setOnClickListener(new View.OnClickListener(){




        public void onClick(View v) {

            btn_onClick();
        }

    }); 



    if (extras != null){

        String[] imglist= extras.getStringArray("IMAGE_LIST");
        imgView.setImageResource(mImageIds[0]);


    }

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
            imgView.setImageResource(mImageIds[position]);




        }
    });


};

private void btn_onClick() {

    Intent intent = new Intent(HelloGallery.this,CameraDemo.class);


    startActivity(intent);
} 

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;


    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}
}
Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
  • Possible duplicate of : http://stackoverflow.com/questions/4497813/how-to-communicate-two-android-applications – Drahakar Nov 24 '11 at 01:07
  • here convert image to bitmap and pass it to another activity [use this answer][1] [1]: http://stackoverflow.com/a/17879210/2377760 – Bald bcs of IT Jul 26 '13 at 11:31

3 Answers3

0

In general, you dont want to be sending an image from one activity to another. You will want activity 1 to store it somewhere, and then tell activity 2 to go and access it. Possibly in another base application class that both activities can access.

Hope this helps

Glenn.nz
  • 2,261
  • 2
  • 17
  • 20
0

In your intent, your have possibility to pass Serializable objects.

Do to, you must create a class for serilizing/deserializing your image like this:

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.Serializable;

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Bitmap.CompressFormat;

    public class SerializableBitmap implements Serializable {
       private static final long serialVersionUID = 1208813848065674061L;
       private Bitmap bitmap;
       // Serialize image
       private void writeObject(java.io.ObjectOutputStream out) throws IOException {
          ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
          bitmap.compress(CompressFormat.JPEG, 100, bos); 
          byte[] bitmapdata = bos.toByteArray();
          out.write(bitmapdata, 0, bitmapdata.length);
       }

       // Deserialize image
       private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
           ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
           int b;
           while((b = in.read()) != -1)
              byteStream.write(b);
           byte bitmapBytes[] = byteStream.toByteArray();
           bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
       }

       public Bitmap getBitmap() {
          return bitmap;
       }

       public void setBitmap(Bitmap bitmap) {
          this.bitmap = bitmap;
       }
   }

Fill free to use setBitmap() and getBitmap() methods to serialize/deserilize your bitmap, and in your intent put the serializable bitmap object:

    intent.putExtra(extraDataKey, serializableBitmap);

But consider that this solution could cause performance problems if the image is too large.

Hope it help.

Hicham
  • 586
  • 3
  • 7
  • [Bitmap](http://developer.android.com/reference/android/graphics/Bitmap.html) has already implemented [Parcelable](http://developer.android.com/reference/android/os/Parcelable.html), directly pass bitmap to intent.putExtra(), do not need wrap it once more. – yorkw Nov 24 '11 at 02:21
  • And then how can I show these images on galleyry in activity1? – Loc Nguyen Nov 24 '11 at 02:24
  • @user1063009 Bitmap image = (Bitmap) getIntent().getExtras().getParcelable(extraDataKey); – yorkw Nov 24 '11 at 02:30
  • Yes, it could work. But I confirm that the solution with SerializableBitmap worked fine for me. I'll try if Parcelable can do the job. Tkank you. – Hicham Nov 24 '11 at 02:37
  • @user1063009: if you have an ImageView then you can do as this: imageView.setImageBitmap(bitmap); – Hicham Nov 24 '11 at 02:40
  • Thank you. But I just want take images to gallery list, then I click on the item on gallery it will show in Imageview. Do you understand me? Wow, so complex :)) – Loc Nguyen Nov 24 '11 at 06:56
0

you send the location of the image like "R.drawable.sample_0" you send it as string and then when you get it in activity 2, you reach it from your drawables . that's all no need to send the image :)

Angie
  • 277
  • 1
  • 7