[Solved] Can you draw a google static map to a canvas after you download it?


As i mentioned in comments, You could actually reuse the bitmap.

You could also draw on canvas directly for date and time since the datetime bitmap will consume 160000 bytes i.e 156kb.

I have not tested the code. This is just a suggestion.

class TheTask extends AsyncTask<Void, Void, Bitmap> {
    File mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + ".png");
    File mediaFile1 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + "1" + ".png");
    //File mediaFile2 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + "2" + ".png");
    Bitmap mapBitmap;

    protected void onPreExecute() {
        super.onPreExecute();
        // display progressdialog.
    }

    protected Bitmap doInBackground(Void... params) {
        mapBitmap = getGoogleMapThumbnail(MainActivity.latlng.latitude, MainActivity.latlng.longitude);
        // You could reuse this bitmap. Rather than saving and loading again
        return mapBitmap;
    }

    protected void onPostExecute(Bitmap map) {
        super.onPostExecute(map);

        Bitmap finished;

        String path = mediaFile1.getAbsolutePath();

        Bitmap photo = BitmapFactory.decodeFile(path);

        Paint into = new Paint();

        Paint background = new Paint();
        background.setColor(Color.WHITE);
        background.setStyle(Style.FILL);

        into.setFilterBitmap(true);

        into.setColor(Color.BLACK);
        into.setTextSize(25);
        into.setUnderlineText(true);
        into.setTypeface(Typeface.DEFAULT_BOLD);

        // Bitmap datetime = Bitmap.createBitmap(200, 200,
        // Bitmap.Config.ARGB_8888);
        //
        // Canvas can = new Canvas(datetime);
        // can.drawColor(Color.WHITE);
        // can.drawText(MainActivity.curDate, 0, 35, into);
        // can.drawText(MainActivity.curTime, 0, 70, into);

        Rect rect = new Rect();

        if (photo.getWidth() > photo.getHeight()) {

            finished = Bitmap.createBitmap(480, 720, Bitmap.Config.ARGB_8888);

            Canvas ca = new Canvas(finished);
            ca.drawColor(Color.WHITE);

            ca.drawBitmap(map, 0, 0, null);

            rect.set(0, 0, 200, 200);
            ca.drawRect(rect, background);
            ca.drawText(MainActivity.curDate, 0, 35, into);
            ca.drawText(MainActivity.curTime, 0, 70, into);

            ca.drawBitmap(photo, 0, 400, null);
        }
        else {

            finished = Bitmap.createBitmap(720, 480, Bitmap.Config.ARGB_8888);

            Canvas ca = new Canvas(finished);
            ca.drawColor(Color.WHITE);

            ca.drawBitmap(map, 320, 0, null);

            ca.save();

            ca.translate(320, 0);
            rect.set(0, 0, 200, 200);
            ca.drawRect(rect, background);
            ca.drawText(MainActivity.curDate, 0, 35, into);
            ca.drawText(MainActivity.curTime, 0, 70, into);

            ca.restore();

            ca.drawBitmap(photo, 0, 0, null);
        }

        mediaFile1.delete();

        FileOutputStream outStream;
        try {
            outStream = new FileOutputStream(mediaFile);
            finished.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Bitmap getGoogleMapThumbnail(double lati, double longi) {
        String URL = "http://maps.google.com/maps/api/staticmap?center=" + lati + "," + longi + "&zoom=15&size=200x200&sensor=false";
        Bitmap bmp = null;
        bmp = getBitmapFromURL(URL);
        return bmp;
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

0

solved Can you draw a google static map to a canvas after you download it?