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


i using following class download specific map , i'd draw canvas other bitmaps when map doesn't show up. know map downloading, have make mutable, i'm not sure what's going on here.

 class thetask extends asynctask<void,void,void>     {     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 bitmap;           protected void onpreexecute()           {           super.onpreexecute();                     //display progressdialog.           }              protected void doinbackground(void ...params)           {                    bitmap = getgooglemapthumbnail(mainactivity.latlng.latitude,mainactivity.latlng.longitude);            fileoutputstream outstream;             try {                 outstream = new fileoutputstream(mediafile2);                 bitmap.compress(bitmap.compressformat.png, 100, outstream);                 outstream.flush();                 outstream.close();             } catch (filenotfoundexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }               //mapview.setdrawingcacheenabled(enabled);             //mediafile = new file(menuscreen.mediastoragedir.getpath() + file.separator +                    // "img_"+ menuscreen.timestamp + ".jpg");                  return null;           }              protected void onpostexecute(void result)           {                           super.onpostexecute(result);                     bitmap finished;                        string path = mediafile1.getabsolutepath();                        string path1 = mediafile2.getabsolutepath();                        bitmap map = bitmapfactory.decodefile(path1);                        bitmap photo = bitmapfactory.decodefile(path);                        paint = new paint();                          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);                           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);                             ca.drawbitmap(datetime, 0, 0, null);                              //ca.drawbitmap(bm, new rect(0,0, bm.getwidth(), bm.getheight()),new rect(280,0,480,200) , null);                              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.drawbitmap(datetime, 320, 0, null);                              //ca.drawbitmap(bm, new rect(0,0, bm.getwidth(), bm.getheight()),new rect(320,280,520,480) , null);                              ca.drawbitmap(photo, 0, 0, null);                         }                           mediafile1.delete();                           mediafile2.delete();                          fileoutputstream outstream;                         try {                             outstream = new fileoutputstream(mediafile);                             finished.compress(bitmap.compressformat.png, 100, outstream);                             outstream.flush();                             outstream.close();                         } catch (filenotfoundexception e) {                             // todo auto-generated catch block                             e.printstacktrace();                         } catch (ioexception e) {                             // todo auto-generated catch block                             e.printstacktrace();                         }                          //sendbroadcast(new intent(                             //    intent.action_media_mounted,                               //  uri.parse("file://" + environment.getexternalstoragedirectory())));                       //dismiss progressdialog.                     //update ui           }             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);                 /*httpclient httpclient = new defaulthttpclient();                   httpget request = new httpget(url);                  inputstream in = null;                try {                    in = httpclient.execute(request).getentity().getcontent();                    bmp = bitmapfactory.decodestream(in);                    in.close();                } catch (illegalstateexception e) {                    // todo auto-generated catch block                    e.printstacktrace();                } catch (clientprotocolexception e) {                    // todo auto-generated catch block                    e.printstacktrace();                } catch (ioexception e) {                    // todo auto-generated catch block                    e.printstacktrace();                }*/                 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;             }         } 

as mentioned in comments, reuse bitmap.

you draw on canvas directly date , time since datetime bitmap consume 160000 bytes i.e 156kb.

i have not tested code. 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);         // reuse bitmap. rather saving , loading again         return mapbitmap;     }      protected void onpostexecute(bitmap map) {         super.onpostexecute(map);          bitmap finished;          string path = mediafile1.getabsolutepath();          bitmap photo = bitmapfactory.decodefile(path);          paint = 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;         }     } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -