title: Loading images from assets
body: <p>i have been working on wallpaper app for a while and it's almost done but the size of app is pretty big cause i use .png extension so currently i'm trying to load jpg via assets instead of png in res</p>
  
  <p>i tried to implement this answer 
  <a href="https://stackoverflow.com/questions/2752924/android-images-from-assets-folder-in-a-gridview">Images from Assets folder in a GridView</a>
  i get an error while loading the imageadapter</p>
  
  <ul>
  <li>02-21 23:13:05.883: E/AndroidRuntime(17634): FATAL EXCEPTION: main</li>
  <li>02-21 23:13:05.883: E/AndroidRuntime(17634): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagieview05/com.example.imagieview05.MainActivity}: java.lang.NullPointerException</li>
  </ul>
  
  <p>here is my code</p>
  
  <pre><code>public class MainActivity extends Activity {
  private GridView mGridView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
       mGridView = (GridView) findViewById(R.id.GridView1);
  
      Bitmap[] mBitArray  = new Bitmap[4];
      try {
      mBitArray[0] = getBitmapFromAssets("g1p2.jpg");
      mBitArray[1] = getBitmapFromAssets("g1p1.jpg");
      mBitArray[2] = getBitmapFromAssets("g1p3.jpg");
      mBitArray[3] = getBitmapFromAssets("g1p4.jpg");
  
      } catch (Exception e) {
           e.printStackTrace();
      }
  
  
    mGridView.setAdapter(new ImageAdapter(this ,mBitArray));
  
  }
  public Bitmap getBitmapFromAssets (String filename) throws IOException{
      AssetManager assetManager = getAssets();
      InputStream istr = assetManager.open(filename);
      Bitmap bitmap = BitmapFactory.decodeStream(istr);
      return bitmap;  
  }
  
  
  
  
    public class ImageAdapter extends BaseAdapter{
  
  
  private Context mContext;
  private Bitmap[] mImageArray;
  
  
  public GallaryAdapter(Context c, Bitmap[] mBitArray) {
      c = mContext;
      mBitArray = mImageArray;
  }
  
  public int getCount() {
      return mImageArray.length;
  }
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return mImageArray[position];
  }
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
  }
  
  public View getView(int position, View convertView, ViewGroup parent) {
       ImageView imgView = new ImageView(mContext);
       imgView.setImageBitmap(mImageArray[position]);
       //put black borders around the image
       imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
       imgView.setLayoutParams(new GridView.LayoutParams(120, 120));
       return imgView;
  
  }
  </code></pre>
  
  <p>here is the  original  working code without Assets reference</p>
  
  <pre><code> public class MainActivity extends Activity {
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      GridView gridView =  (GridView) findViewById(R.id.GridView1);
      gridView.setAdapter(new ImageAdapter(this));
  
  
  public class ImageAdapter extends BaseAdapter {
  
   private Context mContext;
  
  
      public ImageAdapter(Context c) {
          mContext = c;
      }
  
      public int getCount() {
          return mThumbIds.length;
      }
  
      public Object getItem(int position) {
          return null;
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      // create a new ImageView for each item referenced by the Adapter
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView imageView;
          if (convertView == null) {  // if it's not recycled, initialize some attributes
              imageView = new ImageView(mContext);
              imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
              imageView.setPadding(8, 8, 8, 8);
          } else {
              imageView = (ImageView) convertView;
          }
  
          imageView.setImageResource(mThumbIds[position]);
          return imageView;
      }
  
      // references to our images
      private Integer[] mThumbIds = {
              R.drawable.g1p1, R.drawable.g1p2,
              R.drawable.g1p3, R.drawable.g1p4,
              R.drawable.g1p5, R.drawable.g1p6,
              R.drawable.g1p22, R.drawable.g1p33,
              R.drawable.g1p44, R.drawable.g1p55,
              R.drawable.g1p5, R.drawable.g1p6,
              R.drawable.g1p22, R.drawable.g1p33,
              R.drawable.g1p44, R.drawable.g1p55
  
  
  
      };
  };
  </code></pre>
  
  <p>thanks for help in advance </p>