I am implementing a sketch-book in android. In this I have added a canvas. To show that users have multiple pages, when user clicks on next page I save the bitmap currently on canvas to a Bitmap ArrayList and clear the canvas for new bitmap. I am saving whole ArrayList at the end together into image files(Which is working fine).
The problem is, In my app user is able to move to previously drawn bitmaps using Next and Previous buttons, here i did it my redrawing bitmaps from Bitmap ArrayList. That's why I used Bitmap ArrayList. The problem is that after few pages are created like say, 30 pages my app crashes, saying OutOfMemory exception.
Could anyone suggest an efficient way to implement what I am trying to achieve, so that users can create as many pages as they want and still navigate back and forth??
Here is code for saving into bitmap arraylist
public void bitArrayStore(int k) {
if (drawView.canvasBitmap.sameAs(drawView.emptyBitmap)) {
flag = true;
} else {
try {
if (flag1 == false) {
drawView.buildDrawingCache();
drawView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
bitmaps.set(k, Bitmap.createBitmap(drawView.getDrawingCache()));
} else {
bitmaps.add(k, Bitmap.createBitmap(drawView.getDrawingCache()));
flag1 = false;
}
} catch (IndexOutOfBoundsException e) {
bitmaps.add(k, Bitmap.createBitmap(drawView.getDrawingCache()));
}
drawView.destroyDrawingCache();
}
This is the code I use for redrawing into canvas when user clicks previous or next button
public void redraw(ArrayList<Bitmap> bits, int i) {
try {
drawCanvas.drawBitmap(bits.get(i), 0, 0, canvasPaint);
invalidate();
}catch (NullPointerException e){
Log.w("DrawingApp","Exception");
}
}
Here is the code for next and previous button inside onClickListeners
if (v.getId() == R.id.previousbtn) {
PageNoLayout.setVisibility(View.VISIBLE);
hideDrawer(colorNsize);
hideDrawer(eraserDrawer);
try {
bitArrayStore(j);
drawView.startNew();
j--;
if (bitmaps.size() > j) {
drawView.redraw(bitmaps, j);
}
} catch (IndexOutOfBoundsException e) {
j = 0;
try {
drawView.redraw(bitmaps, j);
} catch (IndexOutOfBoundsException e1) {
drawView.startNew();
}
}
pageNo.setText(String.valueOf(j+1));
flag = false;
} else if (v.getId() == R.id.nextbtn) {
PageNoLayout.setVisibility(View.VISIBLE);
hideDrawer(colorNsize);
hideDrawer(eraserDrawer);
if (j < 50) {
bitArrayStore(j);
if (flag == false) {
j++;
}
if (bitmaps.size() > j) {
drawView.startNew();
drawView.redraw(bitmaps, j);
} else {
drawView.startNew();
}
flag = false;
} else {
Snackbar.make(v, "Reached page limit. Please save and start new note", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
pageNo.setText(String.valueOf(j+1));
}