Alex Rivera | Logout

How to Draw something with your finger in an Android App.... And save it to the web

Asked 2012-07-24T13:16:28.890
10

**THIS QUESTION WAS SUCCESSFULLY ANSWERED AND HAS BECOME A BLOG POST <- click **

Hi I am a PHP developer I want to do a simple thing - I want to draw something drawn on a blank page on an Android Phone (with a finger with a largeish "emulated pen nib") and store the bitmap as a jpeg on a server by http post.

Here is what I have so far but this is taken from a tutorial that is involved with writing sprites for a game.. And I cant' adapt it

package com.my.example;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawCapture extends Activity implements OnTouchListener{

    OurView v;
    Bitmap ball;
    float x,y;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_capture);
        v = new OurView(this);
        v.setOnTouchListener(this);
        ball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
        x = y = 0;
        setContentView(v);
    }

    @Override
    protected void onPause(){
        super.onPause();
        v.pause();
    }

    protected void onResume(){
        super.onResume();
        v.resume();
    }

    public class OurView extends SurfaceView implements Runnable{
        Thread t = null;
        SurfaceHolder holder;
        boolean isItOK = false;

        public OurView(Context context) {
            super(context);
            holder =
Edit
Report

1 Answer

2

I'm not sure what part of the "save" you are trying to accomplish, but I'll assume you're asking how to store what you've drawn on the canvas to a bitmap.

First, make yourself a bitmap to draw into. Say, canvasBitmap. Then:

c.setBitmap(canvasBitmap);

This will store everything that has been drawn into 'canvasBitmap.' Then, when the user presses a button to save:

savedBitmap = Bitmap.copy(canvasBitmap.getConfig(), true);

Now, take savedBitmap and fire it off into the cloud. Hope that helps.

answered 2012-07-24T13:37:31.670

Your Answer