title: Gallery inside Dialog, OutOfMemoryError
body: <p>I created simple gallery but when I try to display it after few second when it is visible app crash and I get error : </p>
  
  <pre><code>Throwing OutOfMemoryError "Failed to allocate a 31961100 byte allocation with 16777216 free bytes and 19MB until OOM"
  </code></pre>
  
  <p>I must addmit that it happen when I try to display in gallery just one photo with size less than 1MB</p>
  
  <p>Probably all this happen because I get this .jpg file and it should be decoded Bitmap ? or maybe because I doesn't create thumbnail of it ? Still one photo takes 160RAM?!?! quite strange.  What should I do If I want it to work fast and smooth fow more than 1 picture :). Some example code will be great. </p>
  
  <p>Below all my code to do that</p>
  
  <p>Dialog Gallery : </p>
  
  <pre><code>public void openDialog() {
  
          inflater = this.getLayoutInflater();
  
          // Dialog layout
          v = inflater.inflate(R.layout.dialog_choice, null);
  
          // Get gridView from dialog_choice
          gV = (GridView) v.findViewById(R.id.gridView);
  
          // GridAdapter (Pass context and files list)
          GridAdapter adapter = new GridAdapter(this, photoList);
  
          // Set adapter
          gV.setAdapter(adapter);
  
          final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
          builder2.setTitle("MY GALLERY");
          builder2.setView(v);
          builder2.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
  
              }
          }).setNegativeButton("BACK", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
  
              }
          });
          builder2.setCancelable(false);
          builder2.create().show();
      }
  </code></pre>
  
  <p>GridAdapter : </p>
  
  <pre><code>public class GridAdapter extends BaseAdapter {
  
      Context mContext;
      ArrayList&lt;File&gt; listFiles;
  
      public GridAdapter(Context context, ArrayList&lt;File&gt; files) {
  
          this.mContext = context;
          this.listFiles = files;
      }
  
      @Override
      public int getCount() {
          return listFiles.size();
      }
  
      @Override
      public Object getItem(int position) {
          return listFiles.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          if(convertView == null)
          {
              convertView = LayoutInflater.from(mContext).inflate(R.layout.my_grid, parent, false);
          }
  
          ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
  
          iv.setImageURI(Uri.parse(listFiles.get(position).toString()));
  
          return convertView;
      }
  }
  </code></pre>
  
  <p>Rest used code to do that : </p>
  
  <pre><code>public ArrayList&lt;File&gt; photoList;
  
    @Override public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.guide_photo_album);
          photoList = imageReader(my_root);
  }
  
  
      private ArrayList&lt;File&gt; imageReader(File root)
      {
          ArrayList&lt;File&gt; a = new ArrayList&lt;&gt;();
          File[] files = root.listFiles();
          for(int i=0;i&lt;files.length;i++)
          {
              if(files[i].isDirectory())
              {
                  Bitmap bit = BitmapFactory.decodeFile(files[i].getAbsolutePath());
                  a.addAll(imageReader(files[i]));
              }
              else
              {
                  if (files[i].getName().endsWith(".jpg"))
                  {
                      a.add(files[i]);
                  }
              }
          }
          return a;
      }
  </code></pre>
  
  <p>P.S sorry for weak and chaotic language.</p>