How to pass image using Image Id in intent to different activity?
<p>I am trying to learn to create a meme generator app. First I had a problem with my app crashing when I clicked on the images in the grid view as the images were too big. I was advised to use image id to pass the image to the second activity. I (thought, I) changed the code accordingly. Now when clicked on the image, I can see the next activity with enter top and bottom text options but still image does not appear. I know the problem is the way I am passing image id but don't know what. I hope you can give me specific code as I am very new to programming. </p>

<p>I know using uri also is an option but being a beginner I am not sure how I would execute that here. If you think it is a better (in terms of speed of the app and memory usage / easier to learn and execute), you would be kind enough to help me out with the code too, would be appreciated. </p>

<p>Thank You in advance!</p>

<p>Here is my code:</p>

<p>Main Activity.java</p>

<pre><code>package com.javatechig.gridviewexample;

import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {
    private GridView gridView;
    private GridViewAdapter gridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = (GridView) findViewById(R.id.gridView);
        gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
        gridView.setAdapter(gridAdapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {
                ImageItem item = (ImageItem) parent.getItemAtPosition(position);

                //Create intent
                Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
                intent.putExtra("id", item.getId());

                //Start details activity
                startActivity(intent);
            }
        });
    }

    /**
     * Prepare some dummy data for gridview
     */
    private ArrayList&lt;ImageItem&gt; getData() {
        final ArrayList&lt;ImageItem&gt; imageItems = new ArrayList&lt;&gt;();
        TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
        for (int i = 0; i &lt; imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, R.array.image_ids));
        }
        return imageItems;
    }
}
</code></pre>

<p>DetailsActivity.java</p>

<pre><code>package com.javatechig.gridviewexample;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;

public class DetailsActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_activity);

        //imageUri = Uri.parse(extras.getString("imageUri"));
        Bitmap bitmap = getIntent().getParcelableExtra("id");

        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageBitmap(bitmap);
    }
}
</code></pre>

<p>ImageItem.java</p>

<pre><code>package com.javatechig.gridviewexample;

import android.graphics.Bitmap;

public class ImageItem {
    private Bitmap image;
    private int id;

    public ImageItem(Bitmap image, int id) {
        super();
        this.image = image;
        this.id = id;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }


    public int getId(){
        return id;
    }

    public void setId(int id){
        this.id = id;
    }

}
</code></pre>

<p>GridViewAdapter.java</p>

<pre><code>package com.javatechig.gridviewexample;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import java.util.ArrayList;

public class GridViewAdapter extends ArrayAdapter&lt;ImageItem&gt; {

    private Context context;
    private int layoutResourceId;
    private ArrayList&lt;ImageItem&gt; data = new ArrayList&lt;ImageItem&gt;();

    public GridViewAdapter(Context context, int layoutResourceId, ArrayList&lt;ImageItem&gt; data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) row.findViewById(R.id.image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }


        ImageItem item = data.get(position);
        holder.image.setImageBitmap(item.getImage());
        return row;
    }

    static class ViewHolder {
        ImageView image;
    }
}
</code></pre>