Basic Graphics Stuff

Load image from URL and paste it into your own bitmap

  • Load Image from URL
  • Convert Image to BASE64 – easy storage on sqlite database
  • Convert image to something that can be drawn
    • Of course this is just a toy since converting to base64 and back is silly

      public static Bitmap getBitmapFromURL(String src) {
      	Bitmap ret=null;
          try {
              URL url = new URL(src);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.setDoInput(true);
              connection.connect();
      // get the image
              InputStream input = connection.getInputStream();
      // convert it into byte[] in prep of convert to base64
              byte[] rawimage=readBytes(input); 
      // convert to base64 string
              String x64=Base64.encodeToString(rawimage, Base64.DEFAULT);
      // convert back from base64 
              byte[] ext=Base64.decode(x64, Base64.DEFAULT);
      // render the byte array as a bitmap
              ret = BitmapFactory.decodeByteArray(ext, 0, ext.length);
          } catch (IOException e) {
              e.printStackTrace();
          }
          return ret;
      }
      
      public void paint() throws Exception{
      	   Canvas c = new Canvas(bitmap);
      	   Paint p=new Paint(Color.BLACK);
      	   c.drawColor(Color.WHITE);
      	   p.setTextSize(80);
      	   c.drawText("HEY",50,50,p);
      	   try {
      			//Bitmap timg=getBitmapFromURL("http://www.uclick.com/puzzles/tmjmf/puzzles/tmjmf130501.gif");
      	   //c.save();
      	   c.scale(3f, 3f, 0, 0);
      	   c.drawBitmap(timg, 200, 100, null);
      	   } catch(Exception e){
      		   e.printStackTrace();   
      	   }
      	   //c.restore();
      	
      }