Out Of Memory when selecting images from phone storage

109
June 19, 2017, at 02:21 AM

I am creating an app in which you store information about clients in your phone. I also store bitmap locations to a database that when loaded are thumbnail size in a gridView when viewing the clients information like so

To select the images another activity will start where they can scroll and select which images they would like, with a maximum of 6 images. However after scrolling through a full page full and selecting 6 images to pass to the client activity i experience an Out Of Memory Exception. How can i clear the memory before starting my next activity?

here is my activity in which they select up to 6 images

 public class CustomGalleryActivity extends Activity {
private GridView grdImages;
private Button btnSelect;
private GalleryImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;

/**
 * Overrides methods
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_gallery);
    grdImages= (GridView) findViewById(R.id.grdImages);
    btnSelect= (Button) findViewById(R.id.btnSelect);
    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    @SuppressWarnings("deprecation")
    Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imagecursor.getCount();
    this.arrPath = new String[this.count];
    ids = new int[count];
    this.thumbnailsselection = new boolean[this.count];
    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        ids[i] = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        arrPath[i] = imagecursor.getString(dataColumnIndex);
    }
    imageAdapter = new GalleryImageAdapter();
    grdImages.setAdapter((ListAdapter) imageAdapter);
    imagecursor.close();

    btnSelect.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            selectOnClick();
        }
    });
}
@Override
public void onBackPressed()
{
    setResult(Activity.RESULT_CANCELED);
    super.onBackPressed();
}
/**
 * Class method
 */
/**
 * This method used to set bitmap.
 * @param iv represented ImageView
 * @param id represented id
 */
private void setBitmap(final ImageView iv, final int id) {
    new AsyncTask<Void, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(Void... params) {
            return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
        }
        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            iv.setImageBitmap(result);
        }
    }.execute();
}

/**
 * List adapter
 * @author tasol
 */
public class GalleryImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    public GalleryImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public int getCount()
    {
        return count;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
            holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
            holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.chkImage.setId(position);
        holder.imgThumb.setId(position);

        holder.chkImage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                int id = cb.getId();
                if (thumbnailsselection[id]) {
                    cb.setChecked(false);
                    thumbnailsselection[id] = false;
                } else {
                    cb.setChecked(true);
                    thumbnailsselection[id] = true;
                }
            }
        });
        holder.imgThumb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int id = holder.chkImage.getId();
                if (thumbnailsselection[id]) {
                    holder.chkImage.setChecked(false);
                    thumbnailsselection[id] = false;
                } else {
                    holder.chkImage.setChecked(true);
                    thumbnailsselection[id] = true;
                }
            }
        });

        try {
            setBitmap(holder.imgThumb, ids[position]);
        } catch (Throwable e) {
        }
        holder.chkImage.setChecked(thumbnailsselection[position]);
        holder.id = position;
        return convertView;
    }
}

/**
 * Inner class
 * @author tasol
 */
class ViewHolder {
    ImageView imgThumb;
    CheckBox chkImage;
    int id;
}
public void selectOnClick()
{
    final int len = thumbnailsselection.length;
    int cnt = 0;
    int i;
    String selectImages = "";
    for (i = 0; i < len; i++)
    {
        if (thumbnailsselection[i])
        {
            cnt++;
            selectImages = selectImages + arrPath[i] + "|";
        }
    }
    if (cnt < 6)
    {
        for(i = cnt; i < 6; i++)
        {
            selectImages = selectImages + "blank" + "|";
        }
    }
    if (cnt == 0 )
    {
        selectImages = "blank|blank|blank|blank|blank|blank|";
    }
    if (cnt > 6)
    {
        Toast.makeText(getApplicationContext(), "Please only select 6 images.", Toast.LENGTH_LONG).show();
    }
    else
    {
        Log.d("SelectedImages", selectImages);
        Intent intent = new Intent();
        intent.putExtra("data", selectImages);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
}

thanks for any input or advice!

EDIT

to clarify i can select 6 images just fine with out the OOM exception, but as soon as i scroll through the first page or more i will experience the OOM exception.

READ ALSO
Always Round UP a Double

Always Round UP a Double

How could I always round UP a double to an int, and never round it downI know of Math

112
Attribute getting removed after render

Attribute getting removed after render

I am creating a maze solver with a front end solution

79
Make GridBagLayout JPanel size to contents, not container?

Make GridBagLayout JPanel size to contents, not container?

I have a GridBagLayout JPanel contained within a Box contained within a JTabbedPaneI want the GridBagLayout to size itself to its internal grid

80
Java Gervill Software Synth not working

Java Gervill Software Synth not working

In JDK8, I can't seem to get any useful data out of the AudioInputStream created by a SoftSynthesizer

81