Help on showing app icon in imageview

Hi,I am trying to show app icon from a grid view of installed apps on my phone.However i can’t show image on imageview in next activity on click.If anyone could help me with it?Here is the code

AppGridActivity.java


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class AppGridActivity extends Activity {

GridView mGrid;

ImageView i;
Drawable d;  
Bitmap b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loadApps();

    setContentView(R.layout.activity_main);
    mGrid = (GridView) findViewById(R.id.myGrid);
    mGrid.setAdapter(new AppsAdapter());
}

private List<ResolveInfo> mApps;

private void loadApps() {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}

public class AppsAdapter extends BaseAdapter {

	public AppsAdapter() {
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        CheckableLayout l;
//        ImageView i;

        if (convertView == null) {
            i = new ImageView(AppGridActivity.this);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
            l = new CheckableLayout(AppGridActivity.this);
            l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,
                    GridView.LayoutParams.WRAP_CONTENT));
            l.addView(i);
        } else {
            l = (CheckableLayout) convertView;
            i = (ImageView) l.getChildAt(0);
        }

        final ResolveInfo info = mApps.get(position);
        i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
        
//        BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
//         b = drawable.getBitmap();
//
//        int bytes = b.getByteCount();
//        ByteBuffer buffer = ByteBuffer.allocate(bytes);  
//        b.copyPixelsToBuffer(buffer);  
//        byte[] array = buffer.array();
        

        
        mGrid.setOnItemClickListener(new OnItemClickListener() {


			@Override
			public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
				
				Toast.makeText(AppGridActivity.this, "the position is " + position, Toast.LENGTH_LONG).show();
				
			
				i.buildDrawingCache();
			    Bitmap bitmap = i.getDrawingCache();
//				 Bundle extras = new Bundle();
//				extras.putParcelable("imagebitmap", image);
				Intent intent = new Intent (AppGridActivity.this, MainActivity.class);
//				intent.putExtras(extras);
//				startActivity(intent);
//				ByteArrayOutputStream bs = new ByteArrayOutputStream();
//				b.compress(Bitmap.CompressFormat.PNG, 50, bs);
//				intent.putExtra("byteArray", bs.toByteArray());
//				startActivity(intent);
//				intent.putExtra("id", position);
//				startActivity(intent);
			    intent.putExtra("BitmapImage", bitmap);
			    //				intent.putExtra("id", position);
				startActivity(intent);
				finish();
			}
        }
        );
        return l;
    }

    
   
    

    public final int getCount() {
        return mApps.size();
    }

    public final Object getItem(int position) {
        return mApps.get(position);
    }

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

public class CheckableLayout extends FrameLayout implements Checkable {
    private boolean mChecked;

    public CheckableLayout(Context context) {
        super(context);
    }

    public void setChecked(boolean checked) {
        mChecked = checked;
//            setBackgroundDrawable(checked ?
//                    getResources().getDrawable(R.drawable.blue)
//                    : null);
            setBackground(checked ?
                    getResources().getDrawable(Color.BLACK)
                    : null);


    }

    public boolean isChecked() {
        return mChecked;
    }

    public void toggle() {
        setChecked(!mChecked);
    }
    
    
} 

MainActivity.java

import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.BaseBundle;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button addapp;
	ImageView showapp;
	private List<ApplicationInfo> applist = null;
	private int position;
	private PackageManager packageManager = null;


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

	    addapp = (Button)findViewById(R.id.button1);
	    addapp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent addappintent = new Intent(MainActivity.this,NewGridView.class);
				startActivity(addappintent);
			}
		});
	    
	    

	    showapp = (ImageView)findViewById(R.id.button2);

	     
	     showapp.setOnClickListener(new View.OnClickListener() {
			


			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 ApplicationInfo app = applist.get(position);
				   
				    
				        try {
				
				        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);
				
				        if (null != intent) {
				            startActivity(intent);
				        }
				    } catch (ActivityNotFoundException e) {
				        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
				    } catch (Exception e) {
				        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
				    }
			}
		});
	    

	}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid"
  android:layout_width="match_parent" 
  android:layout_height="match_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"

    android:horizontalSpacing="10dp"
    android:numColumns="4"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"

    android:gravity="center" />

I tried passing image on grid click listener to next activity.but app icon doesn’t show.I have deleted getintent on mainactivity and would like anyone to give me solution thank you

homelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="addapp"
        android:text="Add app" />

     <ImageView
         android:id="@+id/button2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_above="@+id/button1"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="59dp"
         android:text="Show App Icon" />

</RelativeLayout>

Anyone ? It would be helpful