0

I'm fairly new to the android platform and I overrode a CustomView's onDraw method with canvas.drawPath(path, paint) in order to produce drawings on the page.

However, I attempted to save this drawing to be used in another activity (final page) where multiple saved drawings can be displayed side by side, but I ran into frequent issues with no images appearing on my final page.

I was wondering how to handle this scenario. I read posts on SO about storing the drawing as a bitmap and reconstructing this later. So I used Convert view to bitmap on Android

public static Bitmap getBitmapFromView(View view) { ...

to extract a bitmap from my custom view containing the original drawing. Then, I created a canvas with the extracted bit map using

Bitmap map = getBitmapFromView(cView);
Canvas canvas = new Canvas(map);

and appended this canvas to a static list of Canvas objects.

In the activity used to display all drawings side by side, I had

RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_display);
for (int x=0; x < canvasList.size(); x++) {
    Canvas currCanvas = canvasList.get(x) 
    View view = new View(this);
    view.draw(currCanvas);
    layout.addView(canvasList.get(x));
}

There is definitely something wrong with my approach, but I have failed to identify it.

Community
  • 1
  • 1
user1431282
  • 6,535
  • 13
  • 51
  • 68

2 Answers2

1

So, have you tried displaying the bitmapRecieved in getBitmapFromView to verify that it works?

Then the next step would be to store it in a static class, and try to display it in another acitvity to ensure storing works as planned.

And on your last Code Snippet: your are creating a View, drawing onto its canvas and then adding the canvas to the layout? without further research to logical thing to do, seems to add the view to the layout or am i wrong?

Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
1

Try using Bundle savedInstance. Instead of using canvasList, I will use a bitmap array:

    @Override
    protected void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState(state);
        state.putSerializable("starttime", startTime);
        for(int i=0; i<bmp.size; i++){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp[i].compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        state.putByteArray("img"+i, byte[] value);

    }

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

   if ((bundle != null)
           for(int i=0; bundle.getByteArray("img"+i) != null; i++) {
                   //Load byteArray back to bitmap array
           } 
}

I haven't tested the code, so there might be some issues. There is a lot of ways to achieve what you want, you can also save in a drawable or save as a picture at user's client.

Good luck, let me know if it works!

Marco Altran
  • 376
  • 2
  • 9